for..in和for each..in在javascript中有什么区别? [英] What is the difference between for..in and for each..in in javascript?
本文介绍了for..in和for each..in在javascript中有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有细微的差别,我不知道或是相同的,每个浏览器有不同的名称?
解决方案 div
for each ... in 在指定对象属性的所有值上迭代指定变量。
例子:
var sum = 0;
var obj = {prop1:5,prop2:13,prop3:8};
(obj中的var项){
sum + = item;
}
print(sum); //打印26,即5 + 13 + 8
for ... in以任意顺序遍历对象的所有属性指定的变量。
例子:
function show_props(obj,objName){
var result =;
for(var i in obj){
result + = objName +。 + i +=+ obj [i] +\\\
;
}
返回结果;
}
注意03.2013,对于循环中的每个... 都是已弃用。 MDN推荐的新语法是
for ...
。
What is the difference between for..in and for each..in statements in javascript? Are there subtle difference that I don't know of or is it the same and every browser has a different name for it?
解决方案
"for each...in" iterates a specified variable over all values of the specified object's properties.
Example:
var sum = 0;
var obj = {prop1: 5, prop2: 13, prop3: 8};
for each (var item in obj) {
sum += item;
}
print(sum); // prints "26", which is 5+13+8
"for...in" iterates a specified variable over all properties of an object, in arbitrary order.
Example:
function show_props(obj, objName) {
var result = "";
for (var i in obj) {
result += objName + "." + i + " = " + obj[i] + "\n";
}
return result;
}
Note 03.2013, for each... in
loops are deprecated. The 'new' syntax recommended by MDN is for... of
.
这篇关于for..in和for each..in在javascript中有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文