
propertyIsEnumerable用法
語法和功能
obj.propertyIsEnumerable(prop):
判斷prop屬性是否是obj的可枚舉屬性
eg:
var o = {};
var a = [];
o.prop = 'is enumerable';
a[0] = 'is enumerable';
o.propertyIsEnumerable('prop'); //true
a.propertyIsEnumerable(0); //true
Tips 注意事項(xiàng)
-
繼承的屬性顯示為false.必須是 自身的屬性
eg:function A() {} // 構(gòu)造函數(shù)A A.prototype.AMethod = function(){console.log(1)}; function B() {} // 構(gòu)造函數(shù)B B.prototype = new A(); B.prototype.constructor = B; var o = new B(); o.oself = function() {}; o.AMethod(); // 通過原型鏈繼承了AMethod方法 o.propertyIsEnumerable('AMethod'); // false 因?yàn)锳Method是繼承的屬性,所以false o.propertyIsEnumerable('oself'); // true 因?yàn)閛self 是 o 的自身屬性 -
在原型鏈上propertyIsEnumerable不被考慮,盡管constructor可以在for-in循環(huán)中被循環(huán)出來
eg:var a = []; a.propertyIsEnumerable('constructor'); // false a.propertyIsEnumerable('prototype'); // false