1.箭头函数
箭头函数的语法比普通函数更简洁,是一个匿名函数,不能作为构造函数,且不能使用new
//普通函数
let fun = () => {
console.log("箭头函数")
}
//普通函数
function fun(){
console.log("普通函数")
}
箭头函数有两种格式:一种是{。。。}和return ,另一种是把二者省略掉
1-1.不能使用new
let fun = () =>{
console.log("箭头函数");
}
let fn = new fun();
2.箭头函数中没有arguments,可以用展开运算符…解决
1.
function one(a){
console.log(arguments);
}
one(1,2,3,4,5,9);
2.
let two = (...b) => {
console.log(b);
}
b(12,32,54,434);
3.箭头函数的this只指向父级上下文,this的指向取决于定义的父级上下文,跟使用位置没关系,普通函数this谁调用指向谁
var a=200;
let obj={
a:100,
fn:function(){
console.log(this.a);
}
fun:()=>{
console.log(this.a)
}
}
obj.fn();//100
obj.fun();//200
4.箭头函数不能通过call(),apply(),bind()方法直接修改它的this指向。(call,apply,bind会默认忽略第一个参数,但是库正常传参)
let obj={
a:10,
b:function(n){
let f=(n) => n+this.a;
return f(n);
}
c:function (n){
let f=(n)=>n+this.a;
let m={
a:20,
}
return f.call(m,n)
}
}
console.log(obj.b(1))
console.log(obj.c(1))
箭头函数没有原型属性
var a=()=>{
return 1;
}
function b(){
return 2;
}
console.log(a.prototype)
console.log(b.prototype)