如何通過JavaScript的數組成員函數循環,下面的代碼無法正常工作:(JavaScript的數組反射
for (var i in Array.prototype){
alert(i)
} //show nothing
for (var i in []){
alert(i)
} // show nothing
如何通過JavaScript的數組成員函數循環,下面的代碼無法正常工作:(JavaScript的數組反射
for (var i in Array.prototype){
alert(i)
} //show nothing
for (var i in []){
alert(i)
} // show nothing
沒有一個本地原型屬性是枚舉的,但是你可以找到你正在尋找什麼在ECMA規範:
http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf
只能枚舉日您定義的粗糙性,如:
Object.prototype.foo = function(){};
x = {};
for (var prop in x) {
alert(prop);
}
將提醒:
富
另一個有用的技巧是,你可以使用object.hasOwnProperty(property)
一個for..in loop
內分支僅當直接在物體擁有一個屬性,並且該屬性不會從構造器的原型下降,其中所有對象都從Object.prototype
下降很多。
您無法通過本地方法循環。