Access object properties within object [duplicate]

CommunityAdnan提出了一个问题:Access object properties within object [duplicate],或许与您遇到的问题类似。

回答者I Hate Lazy给出了该问题的处理方式:

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;
};

希望本文对你有帮助,欢迎支持JavaScript中文网