Access object properties within object [duplicate]

You can’t reference an object during initialization when using object literal syntax. You need to reference the object after it is created.

settings.birthplace = settings.country;

Only way to reference an object during initialization is when you use a constructor function.

This example uses an anonymous function as a constructor. The new object is reference with this.

var settings = new function() {
    this.user = "someuser";
    this.password = "password";
    this.country = "Country";
    this.birthplace = this.country;
};

Leave a Comment