本文介绍了.forEach 中 thisArg 的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavaScript 的 forEach 文档 指出.forEach 语法是:

JavaScript's forEach documentation states that the .forEach syntax is:

arr.forEach(callback[, thisArg])

thisArg有什么用?

推荐答案

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)

这篇关于.forEach 中 thisArg 的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:17
查看更多