如何在对象中创建javascript对象?

Ern*_*est 1 javascript object

例如:

function userObject(start_value) {  
    this.name = start_value;
    this.address = start_value;
    this.cars = function() {
        this.value = start_value;
        this.count = start_value;
    };
}
Run Code Online (Sandbox Code Playgroud)

显然,上述剂量工作,但将欣赏汽车可用的方向:userObject.cars.value = 100000;

干杯!

小智 5

请记住,函数(以及"对象定义")可以嵌套(绝对不要求嵌套,但是嵌套在这里允许闭包start_value):

function userObject(start_value) {  
    this.name = start_value;
    this.address = start_value;
    function subObject () {
        this.value = start_value;
        this.count = start_value;
    }
    this.cars = new subObject();
}
Run Code Online (Sandbox Code Playgroud)

但是,我可能会选择这个(只需创建一个新的"普通"对象):

function userObject(start_value) {  
    this.name = start_value;
    this.address = start_value;
    this.cars = {
        value: start_value,
        count: start_value
    };
}
Run Code Online (Sandbox Code Playgroud)

快乐的编码.