如何在傳入 Array.forEach() 的 Function 使用 this ?

栏目: JavaScript · 发布时间: 7年前

内容简介:ECMAScript 5ECMAScript 2015

Array.prototype 下的 method 都要我們傳入 Function,若該 Function 含有 this 想讀取 Object 的 Property,就會出現錯誤,該怎麼解決這個常見的錯誤呢 ?

Version

ECMAScript 5

ECMAScript 2015

For Loop

const student = {
  name: 'Sam',
  friends: ['Kevin', 'John', 'Mike'],
  sayHello: function() {
    for(let i = 0; i < this.friends.length; i++)
      console.log(`${this.name} say Hello to ${this.friends[i]}`);
  }
};

student.sayHello();

一個常見的 OOP 需求,object 內有 property,而 method 想要讀取 property 的值。

若使用 for loop 搭配 this 讀取 property,則完全沒有問題。

如何在傳入 Array.forEach() 的 Function 使用 this ?

const student = {
  name: 'Sam',
  friends: ['Kevin', 'John', 'Mike'],
  sayHello: function() {
    this.friends.forEach(function(friend) {
      console.log(`${this.name} say Hello to ${friend}`);
    });
  }
};

student.sayHello();

若將 for loop 改成 Array.prototype 的 forEach() 後, this 成為 undefined

因為 sayHello()student 的 method,所以 this 指向 student ,但傳入 forEach() 的 function 並不是 student 的 method,所以 this 指向 undefined

這該怎麼解決呢 ?

Self

const student = {
  name: 'Sam',
  friends: ['Kevin', 'John', 'Mike'],
  sayHello: function() {
    const self = this;
    this.friends.forEach(function(friend) {
      console.log(`${self.name} say Hello to ${friend}`);
    });
  }
};

student.sayHello();

最傳統、最常見的方法就是將 this 指定給 self ,如此傳入 forEach() 的 function 可透過 Closure (Lexical Scope) 存取 self

如何在傳入 Array.forEach() 的 Function 使用 this ?

Bind

const student = {
  name: 'Sam',
  friends: ['Kevin', 'John', 'Mike'],
  sayHello: function() {
    this.friends.forEach(function(friend) {
      console.log(`${this.name} say Hello to ${friend}`);
    }.bind(this));
  }
};

student.sayHello();

既然傳入 forEach() 的 function 的 this 不見了,我們可以透過 bind() 再度將 this 綁定進去。

如何在傳入 Array.forEach() 的 Function 使用 this ?

forEach 傳入 this

const student = {
  name: 'Sam',
  friends: ['Kevin', 'John', 'Mike'],
  sayHello: function() {
    this.friends.forEach(function(friend) {
      console.log(`${this.name} say Hello to ${friend}`);
    }, this);
  }
};

student.sayHello();

其實 Array.prototype 下的 method,除了傳入 function 外,還有第二個參數,可以將 this 傳進去。

如何在傳入 Array.forEach() 的 Function 使用 this ?

Arrow Function

const student = {
  name: 'Sam',
  friends: ['Kevin', 'John', 'Mike'],
  sayHello: function() {
    this.friends.forEach(friend => console.log(`${this.name} say Hello to ${friend}`));
  }
};

student.sayHello();

最漂亮的解法是改用 ECMAScript 2015 的 Arrow Function,它沒有自己的 this ,而會使用 parent scope 的 this ,也就是 sayHello()this ,因此可以順利讀取到 object 的 property。

Conclusion

  • 只要在 object 的 method 中,傳入 Anonymous Function 到其他 function 中,卻又試圖存取 object 的 property,就會踩到這個雷,有多種解法可以解決,但最優雅的就是 ECMAScript 2015 的 Arrow Function

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

自品牌

自品牌

[美] 丹·斯柯伯尔(Dan Schawbel) / 佘卓桓 / 湖南文艺出版社 / 2016-1-1 / 39.80元

什么是自品牌?如何利用新媒体推广自己?如何放大自己的职业优势? 细化到如何巩固“弱联系”人脉?如何在团队里合作与生存?如何开创自己的事业?这些都是职场人不得不面临的问题,但少有人告诉你答案,你需要利用书里分享的高效方法独辟蹊径,把自己变成职场里高性价比的人才。这是一本教你利用新型社交媒体开发职业潜能的自我管理读本,不管你是新人还是老鸟,都可以通过打造自品牌在职场中脱颖而出。如果不甘平庸,就亮......一起来看看 《自品牌》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具