美文网首页
propertyIsEnumerable() 用法

propertyIsEnumerable() 用法

作者: 小菜101 | 来源:发表于2017-10-24 17:09 被阅读0次

    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

    相关文章

      网友评论

          本文标题:propertyIsEnumerable() 用法

          本文链接:https://www.haomeiwen.com/subject/ifdxpxtx.html