Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js 中的箭头函数 #20

Open
negativeentropy9 opened this issue Aug 18, 2019 · 0 comments
Open

js 中的箭头函数 #20

negativeentropy9 opened this issue Aug 18, 2019 · 0 comments

Comments

@negativeentropy9
Copy link
Owner

negativeentropy9 commented Aug 18, 2019

js 中的箭头函数

call 或 apply 调用

由于 箭头函数没有自己的this指针,通过 call() 或 apply() 方法调用一个函数时,只能传递参数,他们的第一个参数会被忽略。

var adder = {
  base : 1,

  add : function(a) {
    var f = v => v + this.base;
    return f(a);
  },

  addThruCall: function(a) {
    var f = v => v + this.base;
    var b = {
      base : 2
    };

    return f.call(b, a);
  }
};

console.log(adder.add(1));         // 输出 2
console.log(adder.addThruCall(1)); // 仍然输出 2

参考

@negativeentropy9 negativeentropy9 changed the title 箭头函数 js 中的箭头函数 Aug 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant