自定义 属性 在 Javascript 中不可枚举?

Customized property is not enumerable in Javascript?

我定义了自己的"Age"类型,作为"Person"类型的一部分,像这样:

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

我的期望是,只要o1的年龄属性是从"Age"创建的,而它的"year/month"都可以用字符串作为索引访问,那么o1就是可枚举类型. 但事实上,它打印 "false".

为什么会这样,是不是我的理解有误?

您定义的是全局变量而不是属性

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

应该是

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