兩組代碼有什么區別,我知道箭頭函式是做什么的,但為什么傳統運算式不起作用?
套裝 - 1
回圈遍歷所有元素
checkboxes.forEach( function (checkbox){
console.log(checkbox);
if( checkbox === this || checkbox === lastChecked)
{
inBetween = !inBetween;
console.log(" start checking them inbetween");
}
if(inBetween)
{
checkbox.checked = true;
}
});
套裝 - 2
回圈遍歷所有元素
checkboxes.forEach(checkbox => {
console.log(checkbox);
if (checkbox === this || checkbox === lastChecked) {
inBetween = !inBetween;
console.log('Starting to check them in between!');
}
if (inBetween) {
checkbox.checked = true;
}
});
uj5u.com熱心網友回復:
Set - 1 是具有 ES5 定義風格的 foreach,而 Set-2 是針對 ES6 的。確保目標/版本支持 ES5,以便 Set-1 可以作業。
此外,您可能想嘗試在 set-1中用self替換this并在 foreach 之前將 self 定義為 this 或使用 bind
var self = this;
this.addNewObjects = function(arr){
arr.forEach(function(obj) {
self.addObject(new Obj(obj.prop1, obj.prop2));
});
}
或者
this.addNewObjects = function(arr) {
arr.forEach(function(obj) {
this.addObject(new Obj(obj.prop1, obj.prop2));
}.bind(this));
}
uj5u.com熱心網友回復:
我認為,可能是您this在forEach()功能范圍內使用,這是兩者中的一個。
更多解釋是:https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/372667.html
標籤:javascript 网络
