且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.forEach 中 thisArg 的目的是什么?

更新时间:2022-06-12 05:18:27

thisArg 指的是应该调用回调的上下文,基本上它就是 this 在回调中引用的内容.例如:

thisArg refers to context which callback should be called, basically it is what this refers to inside callback. For example:

var myObject = { name: 'myObject' };

[1,2].forEach(function(item) { 
 console.log(item); // 1, 2
 console.log(this === myObject); // true
}, myObject)