Javascript 这篇文章的目的是什么?
JavaScript声明Javascript 这篇文章的目的是什么?,javascript,foreach,Javascript,Foreach,JavaScript声明.forEach语法为: arr.forEach(callback[, thisArg]) thisArg的用法是什么?thisArg指的是应该调用回调的上下文, 基本上这就是这个所指的内部回调。例如: var myObject = { name: 'myObject' }; [1,2].forEach(function(item) { console.log(item); // 1, 2 console.log(this === myObject); // t
.forEach
语法为:
arr.forEach(callback[, thisArg])
thisArg
的用法是什么?thisArg
指的是应该调用回调的上下文,
基本上这就是这个所指的内部回调。例如:
var myObject = { name: 'myObject' };
[1,2].forEach(function(item) {
console.log(item); // 1, 2
console.log(this === myObject); // true
}, myObject)
此
值是一个特殊对象,它与执行上下文
相关
在其中激活执行上下文的对象
此
的值在输入上下文时仅确定一次
而且不可能给这个赋值
在您的情况下,提供thisArg
类似于
arr.forEach(callback.bind(thisArg));
forEach
,为您简化了它,要求一个单独的可选参数
现在,如果您在运行此forEach时不使用this
arr.forEach(function(item) {
console.log(this === window); //true
console.log(this === arr); //false
});
你明白了 可以提供thisArg
来更改回调函数的内部this
。见下文
如果您对使用箭头功能时,thisArg
不起任何作用这一事实感到困惑:
var myObject = { name: 'myObject' };
[1,2].forEach(item => {
console.log(item); // 1, 2
console.log(this === myObject, this); // false Window {}
}, myObject)
因为
无法绑定箭头函数
具有正常功能的上下文绑定。
如果此时不指定myObject
,内部的this
将指向窗口,就像箭头功能一样。我认为这些测试将使整个事情变得清楚
只需在浏览器控制台中进行测试
arr=[0];
arr.forEach(function(item) {
console.log(this === window); //true
console.log(this === arr); //false
});
arr.forEach(function(item) {
console.log(this === window); //true
console.log(this === arr); //false
},this);
arr.forEach(function(item) {
console.log(this === window); //false
console.log(this === arr); //true
},arr);
arr.forEach(function(item) {
console.log(this === window); //false
console.log(this === arr); //false
},0);
我经常在原型和函数的上下文中使用:
var MyClass=function(){
这个.GlobalVar=3;
}
MyClass.prototype.func1=函数(a){
return(a==this.GlobalVar);
}
MyClass.prototype.func2=函数(arr){
arr.forEach(功能(项目){
console.log(this.func1(项));
},this);//thisArg的用法
}
MyClass.prototype.func3=函数(arr){
var=这个;
arr.forEach(功能(项目){
console.log(即.func1(项));
});//也可以不使用thisArg
}
MyClass.prototype.func4=函数(arr){
arr.forEach(功能(项目){
console.log(this.func1(项));
});//实现引发错误,因为该上下文中不存在this.func1
}
var-arr=[0,1,3,4,3,1];
var myClass=新的myClass();
myClass.func2(arr);
/*印刷品:
假的
假的
真的
假的
真的
假的
*/
myClass.func3(arr)/*与func2相同*/
myClass.func4(arr);/*失败于
错误:{
“消息”:“TypeError:this.func1不是函数”,
“文件名”:”https://stacksnippets.net/js",
“lineno”:35,
“科尔诺”:26
}
*/
如文档所述可选。值在执行回调时用作此值。
-如果(arguments.length>1){T=thisArg;}
我投票结束这个问题,因为它要求解释已经很好地记录的内容。@LoïcFaure Lacroix你在说什么?我问了一个文档的例子,这对于meA来说并不清楚,它给了你很多例子。注意:如果你不指定它,这个
将仅在非严格模式下成为全局对象,在严格模式下它将是未定义的
。如果arr
是全局对象并且从数组继承,则此==arr
可能为真。原型和模式不严格。
arr=[0];
arr.forEach(function(item) {
console.log(this === window); //true
console.log(this === arr); //false
});
arr.forEach(function(item) {
console.log(this === window); //true
console.log(this === arr); //false
},this);
arr.forEach(function(item) {
console.log(this === window); //false
console.log(this === arr); //true
},arr);
arr.forEach(function(item) {
console.log(this === window); //false
console.log(this === arr); //false
},0);