笔记:箭头函数。。。

    技术2022-08-01  67

    箭头函数:

    没有argument的概念,有了rest剩余参数的概念

    x var add=(x,y)=>{ console.log(arguments) } add(1,2,3,4)//arguments is not defined//没有定义 var add=(x,y,...rest)=>{ console.log(rest)//[3,4] a.push(x,y);//把所有的参数都遍历出来,这是在之后追加[3,4,1,2] a.unshift(x,y);//这是之前追加[1,2,3,4],这两个都可以得到arguments效果 } add(1,2,3,4)//rest存多余的参数

    call apply bind区别:

    //this指向 //事件中的this,指向事件源 //普通函数的this指向window //普通函数: function add(){ console.log(this) } add()//window //call修改this的指向 var stu={ name='zs } function add(){ console.log(this)//{name='zs'} } add(); add.call(stu); var stu={ name='zs } function add(){ console.log(x,y); console.log(this)//{name='zs'} } //函数名.call(对象,实参)直接调用函数,修改this指向,指向的就是传递的对象 //add.call(stu,1,2); //函数名.apply(对象,[实参])直接调用函数,修改this指向,指向的就是传递的对象 //add.apply(stu,[1,2]); //函数名.bind(对象,实参)返回一个新的函数,不会直接调用,修改this指向,指向的就是传递的对象 fn=add.bind(stu,1,2); fn()

    箭头函数:箭头函数中this,指向的是定义的时候所在对象,不是使用的时候所在对象

    //箭头函数:箭头函数中this,指向的是定义的时候所在对象,不是使用的时候所在对象 var stu={ name='zs } var add=(x,y,...rest)=>{ console.log(this) console.log(rest) a.push(x,y); a.unshift(x,y); }//这个箭头函数的普通函数定义(的时候指向的window add(1,2,3,4) add.call(stu,1,2);//加不加这个都是指向window
    Processed: 0.010, SQL: 9