我想知道forEach回调函数中的'this'值(或调用上下文)是什么。这段代码似乎不起作用:
var jow = [5, 10, 45, 67];
jow.forEach(function(v, i, a){
this[i] = v + 1;
});
alert(jow);
感谢您向我解释。
我想知道forEach回调函数中的'this'值(或调用上下文)是什么。这段代码似乎不起作用:
var jow = [5, 10, 45, 67];
jow.forEach(function(v, i, a){
this[i] = v + 1;
});
alert(jow);
感谢您向我解释。
MDN指出:
array.forEach(callback[, thisArg])
如果提供了thisArg参数,则它将被用作forEach中每个回调函数调用的this值,就好像调用callback.call(thisArg, element, index, array)一样。如果thisArg为undefined或null,则在函数内部,this的值取决于函数是否处于严格模式(在严格模式下传递的值,在非严格模式下使用全局对象)。
因此简而言之,如果只提供了回调函数并且您处于非严格模式下(您提出的情况),则它将是全局对象(window)。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
this
确实是您特定的数组,但它与您提供的回调之间没有任何连接。回调会以forEach想要的方式被调用,具体取决于thisArg
、window
或元素。 - Tibos我完成了forEach方法的构建,并希望与大家分享这个图表,希望能帮助其他人理解其内部工作原理。
this
指向全局的 window
对象。即使你从不同的对象(例如一个你创建的对象)调用它,这种情况仍然如此。window.foo = 'window';
var MyObj = function(){
this.foo = 'object';
};
MyObj.prototype.itirate = function () {
var _this = this;
[''].forEach(function(val, index, arr){
console.log('this: ' + this.foo); // logs 'window'
console.log('_this: ' + _this.foo); // logs 'object'
});
};
var newObj = new MyObj();
newObj.itirate();
// this: window
// _this: object
thisArg
参数来定义 this
指向的对象。在你的例子中,你可以调用 [''].forEach(function (…) {…}, myObj)
,这样 this.foo
就会返回 'object'
而不是 'window'
。 - chharveythisArg
引用全局 window
对象(我已在浏览器中测试过),而规范中说如果没有提供 thisArg
,则默认应该为 undefined
。MDN:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach。规范:https://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.18 - chharvey如果你不向forEach
传递第二个参数,this
将指向全局对象。为了实现你想要做的事情,
var jow = [5, 10, 45, 67];
jow.forEach(function(v, i, a) {
a[i] = v + 1;
});
console.log(jow);
输出
[ 6, 11, 46, 68 ]
let obj = { name:"test", fun:printName }
function printName(){
console.log(this.name)
}
//who is left to the caller? obj! so obj will be 'this'
obj.fun() //test
//who is left to the caller? global! so global will be 'this'
printName() //undefined (global has no name property)
所以,在使用“foreach”时,如果您提供一个回调函数,实际上在“foreach”实现中会发生以下情况:
--> 您调用[1,2,3].foreach(callback,“可选This”)
foreach(arr,cb)
{
for(i=0; i<arr.len;i++)
{
//who is left to the caller? global! so it will be 'this'
cb(arr[i])
}
}
除非您使用可选的“this”或者使用箭头函数绑定回调函数,否则会出现这种情况,那么被调用的回调函数已经具有一个“this”对象,这在某种程度上“阻止”您更改其给定的上下文。更多关于bind的信息可以在这里找到 enter link description here。但基本上,bind的实现如下:
Function.prototype.bind = function (scope) {
var fn = this;
return function () {
return fn.apply(scope);
};
}
因此,您可以看到fn(回调函数)始终将使用您的“this”(作用域)调用。
希望对您有所帮助...