this.foo
is undefined because in all your examples, this
is referring to the global window
object. Also, even if you tried obj1.foo
, it will still return undefined because the property hasn't been created until the entire expression is evaluated. Try this instead:
var obj1 = {
foo: new Date(),
bar: function() {
return new MyDate( this.foo ); // will work
}
};
It works because by the time you call obj1.bar()
, the object will have been created by then; and because you're in a function, the this
object will reference the current object.