javascript defineProperty to make an attribute non enumerable

后端 未结 1 1001
借酒劲吻你
借酒劲吻你 2021-01-18 01:19

I\'m trying to use defineProperty to made attributes not appear in for...in cycle, but it doesn\'t work. Is this code correct?

function Item() {
    this.enu         


        
相关标签:
1条回答
  • 2021-01-18 01:35

    Item does not have a property named nonEnum (check it out). It is a (constructor) function that will create an object that has a property called nonEnum.

    So this one would work:

    var test = new Item();
    Object.defineProperty(test, "nonEnum", { enumerable: false });
    

    You could also write this function like this:

    function Item() {
        this.enumerable = "enum";
        Object.defineProperty(this, "nonEnum", { 
            enumerable: false, 
            value: 'noEnum' 
        });
    }
    

    jsFiddle Demo

    0 讨论(0)
提交回复
热议问题