不清楚这个javascript代码是如何工作的。
我想知道这个foreach循环是如何工作的(请参阅reset函数)。 我猜我可以调用某个PropertyManager.reset();它将在其上执行foreach循环。我不明白循环中发生了什么不清楚这个javascript代码是如何工作的。,javascript,Javascript,我想知道这个foreach循环是如何工作的(请参阅reset函数)。 我猜我可以调用某个PropertyManager.reset();它将在其上执行foreach循环。我不明白循环中发生了什么 PropertiesManager = function() { this.controls = {}; this.controlNames = []; }; PropertiesManager.prototype = { // code block removed //
PropertiesManager = function() {
this.controls = {};
this.controlNames = [];
};
PropertiesManager.prototype = {
// code block removed //
reset: function(selectedControls) {
var controls = this.controls;
**Array.forEach(selectedControls || this.controlNames, function(control) {
controls[control].reset();
});**
}
};
selectedControls | | this.controlNames
表示“循环selectedControls
,但如果为空或未定义,则循环this.controlNames
”
Array.forEach()
的第二个参数是为数组中的每个条目运行的函数,其参数是数组中的当前项(控件)。selectedControls | | this.controlNames
表示“循环selectedControls
,但如果它为null或未定义,则循环this.controlNames
”
Array.forEach()
的第二个参数是为数组中的每个条目运行的函数,其参数是数组中的当前项(控件)。让我以更详细的方式重新编写该函数:
reset: function(selectedControls) {
var controls = this.controls;
var arrayToIterate;
if (selectedControls) {
arrayToIterate = selectedControls;
} else {
arrayToIterate = this.controlNames;
}
Array.forEach(arrayToIterate, function(control) {
controls[control].reset();
});
}
|
运算符是或
运算符。如果第一个值为falsy,则将使用第二个值undefined
,null
,以及一些其他值都被限定为false
,这是一个比false更宽松的定义。
让我以更详细的方式重新编写该函数:
reset: function(selectedControls) {
var controls = this.controls;
var arrayToIterate;
if (selectedControls) {
arrayToIterate = selectedControls;
} else {
arrayToIterate = this.controlNames;
}
Array.forEach(arrayToIterate, function(control) {
controls[control].reset();
});
}
|
运算符是或
运算符。如果第一个值为falsy,则将使用第二个值undefined
、null
,以及少数其他值都符合false
,这是一个比false更宽松的定义。
forEach函数用于对数组中的每个值执行一次或多次函数。
例如:
function logResult(element) {
console.log(element);
}
//and then performing this operation:
["Apples", "Oranges", "Bananas"].forEach(logResult);
//Would log "Apples", then "Oranges", and then "Bananas" to the console.
示例中的处理是检查是否定义了selectedControls
。如果是,则在其上循环。如果不是,则循环此.controlNames
参考:
forEach函数用于对数组中的每个值执行一次一个或多个函数。 例如:
function logResult(element) {
console.log(element);
}
//and then performing this operation:
["Apples", "Oranges", "Bananas"].forEach(logResult);
//Would log "Apples", then "Oranges", and then "Bananas" to the console.
示例中的处理是检查是否定义了selectedControls
。如果是,则在其上循环。如果不是,则循环此.controlNames
参考:
这里forEach从索引0开始一次(在每个循环中)获取数组的每个元素,并在第一个循环中打印“a”,在第二个循环中打印“b”,在第三个循环中打印“c”
控制台中的输出:a b c
在代码中:
Array.forEach(selectedControls || this.controlNames, function(control) {
controls[control].reset();
});
它需要selectedControls
array或object或controlNames
array或object(如果selectedControls为null)并通过此循环。control
是数组元素,在每个循环中一次迭代
这里forEach从索引0开始一次(在每个循环中)获取数组的每个元素,并在第一个循环中打印“a”,在第二个循环中打印“b”,在第三个循环中打印“c”
控制台中的输出:a b c
在代码中:
Array.forEach(selectedControls || this.controlNames, function(control) {
controls[control].reset();
});
它采用
selectedControls
array或object或controlNames
array或object(如果selectedControls为null)并循环执行此操作。control
是数组元素,在每个循环中一次迭代。它将循环执行selected controls
(如果存在),或this.controlNames
否则。然后,对于每个控件,它在控件
字典中获取相应的条目,并调用自己的重置函数。如果存在所选控件
,它将循环通过该控件,否则它将循环通过所选控件。然后,对于每个控件,它在控件
字典中获取相应的条目,并调用自己的重置函数。