propertyIsEnumerable的用法

来源:互联网 发布:淘宝客服兼职怎么做 编辑:程序博客网 时间:2024/06/04 00:49

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 注意事项

  1. 继承的属性显示为false.必须是 自身的属性
    eg:

        function A() {}  // 构造函数A    A.prototype.AMethod = function(){console.log(1)};    function B() {}  // 构造函数B    B.prototype = new A();    B.prototype.constructor = B;    var o = new B();    o.oself = function() {};    o.AMethod(); // 通过原型链继承了AMethod方法    o.propertyIsEnumerable('AMethod');  // false  因为AMethod是继承的属性,所以false    o.propertyIsEnumerable('oself'); // true 因为oself 是 o 的自身属性
  2. 在原型链上propertyIsEnumerable不被考虑,尽管constructor可以在for-in循环中被循环出来
    eg:

        var a = [];    a.propertyIsEnumerable('constructor'); // false    a.propertyIsEnumerable('prototype');  // false

TAHT ALL