箭头函数

箭头函数:

箭头函数提供了一种更加简洁的函数书写方式,基本语法是:

参数 => 函数体

基本用法:

var f = v => v;//等价于var f = function(a){ return a;}f(1);  //1

当箭头函数没有参数或者有多个参数,要用 () 括起来。

var f = (a,b) => a+b;f(6,2);  //8

当箭头函数函数体有多行语句,用 {} 包裹起来,表示代码块,当只有一行语句,并且需要返回结果时,可以省略 {} , 结果会自动返回。

var f = (a,b) => { let result = a+b;

return result;}f(6,2);  // 8

当箭头函数要返回对象的时候,为了区分于代码块,要用 () 将对象包裹起来

// 报错var f = (id,name) => {id: id, name: name};f(6,2);  // SyntaxError: Unexpected token : // 不报错var f = (id,name) => ({id: id, name: name});f(6,2);  // {id: 6, name: 2}

注意点:没有 this、super、arguments 和 new.target 绑定。

var func = () => {  // 箭头函数里面没有 this 对象,  // 此时的 this 是外层的 this 对象,即 Window  console.log(this)}func(55)  // Window  var func = () => {   

  console.log(arguments)}func(55);  // ReferenceError: arguments is not defined

箭头函数体中的 this 对象,是定义函数时的对象,而不是使用函数时的对象。

function fn(){  setTimeout(()=>{    // 定义时,this 绑定的是 fn 中的 this 对象    console.log(this.a);

  },0)}var a = 20;// fn 的 this 对象为 {a: 18}fn.call({a: 18});  // 18

不可以作为构造函数,也就是不能使用 new 命令,否则会报错

适合使用的场景

ES6 之前,JavaScript 的 this 对象一直很令人头大,回调函数,经常看到 var self = this 这样的代码,为了将外部 this 传递到回调函数中,那么有了箭头函数,就不需要这样做了,直接使用 this 就行。

// 回调函数var Person = {    'age': 18,

    'sayHello': function () {      setTimeout(function () {        console.log(this.age);

      });

    }};var age = 20;Person.sayHello();  // 20 var Person1 = {    'age': 18,

    'sayHello': function () {      setTimeout(()=>{        console.log(this.age);

      });

    }};var age = 20;Person1.sayHello();  // 18

所以,当我们需要维护一个 this 上下文的时候,就可以使用箭头函数。

箭头函数与普通函数的区别

箭头函数写法:

let fun = () => {

    console.log('lalalala');

}

普通函数写法:

function fun() {

    console.log('lalla');

}

箭头函数相当于匿名函数,并且简化了函数定义。箭头函数有两种格式,一种只包含一个表达式,连{ ... }和return都省略掉了。还有一种可以包含多条语句,这时候就不能省略{ ... }和return。

箭头函数是匿名函数,不能作为构造函数,不能使用new

let FunConstructor = () => {

    console.log('lll');

}

let fc =newFunConstructor();

箭头函数不绑定arguments,取而代之用rest参数...解决

function A(a){

  console.log(arguments);

}

A(1,2,3,4,5,8);//  [1, 2, 3, 4, 5, 8, callee: ƒ, Symbol(Symbol.iterator): ƒ]let B = (b)=>{

  console.log(arguments);

}

B(2,92,32,32);// Uncaught ReferenceError: arguments is not definedlet C = (...c) => {

  console.log(c);

}

C(3,82,32,11323);// [3, 82, 32, 11323]

箭头函数不绑定this,会捕获其所在的上下文的this值,作为自己的this值

varobj = {

  a: 10,

  b: () => {

    console.log(this.a);// undefinedconsole.log(this);// Window {postMessage: ƒ, blur: ƒ, focus: ƒ, close: ƒ, frames: Window, …}  },

  c: function() {

    console.log(this.a);// 10console.log(this);// {a: 10, b: ƒ, c: ƒ}  }

}

obj.b();

obj.c();

varobj = {

  a: 10,

  b: function(){

    console.log(this.a);//10  },

  c: function() {

    return()=>{

          console.log(this.a);//10    }

  }

}

obj.b();

obj.c()();

箭头函数通过call()或 apply()方法调用一个函数时,只传入了一个参数,对this并没有影响。

let obj2 = {

    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(obj2.b(1));// 11console.log(obj2.c(1));// 11

箭头函数没有原型属性

vara = ()=>{

  return1;

}function b(){

  return2;

}

console.log(a.prototype);  // undefinedconsole.log(b.prototype);// {constructor: ƒ}

箭头函数不能当做Generator函数,不能使用yield关键字

总结

箭头函数的this永远指向其上下文的this,任何方法都改变不了其指向,如call(),bind(),apply()

普通函数的this指向调用它的那个对象

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容