Search code examples
javascripttypespropertiescustomizationenumerable

Customized property is not enumerable in Javascript?


I defined my own "Age" type,as part of "Person" type, like this:

var Age=function(){
    year='1930',
    month='Jan'
}
var Person=function(){
    name='abc',
    age=new Age()
}
var o1=new Person()
console.log(o1.propertyIsEnumerable('age'))

My expectation is, as long as o1's age property is created from "Age", while its "year/month" can both be visited using string as index, then o1 is of enumerable type. But the fact it, it prints "false".

Why is that, is my understanding incorrect?


Solution

  • You are defining global variables not properties

    var Age=function(){
        year='1930',
        month='Jan'
    }
    var Person=function(){
        name='abc',
        age=new Age()
    }
    

    Should be

    var Age=function(){
        this.year='1930';
        this.month='Jan';
    }
    var Person=function(){
        this.name='abc';
        this.age=new Age();
    }