-1

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?

Troskyvs
  • 7,537
  • 7
  • 47
  • 115
  • Please re-read the part of the JS tutorial where it talks about constructors and how to set instance properties. –  Aug 05 '16 at 10:48

1 Answers1

1

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();
}
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98