[英]How to create a javascript object within an object?
For example: 例如:
function userObject(start_value) {
this.name = start_value;
this.address = start_value;
this.cars = function() {
this.value = start_value;
this.count = start_value;
};
}
Obviously the above dosent work but would appreciate the direction to take to have cars available as: userObject.cars.value = 100000; 显然,上述工作很有效,但是希望可以了解将汽车提供给的方向:userObject.cars.value = 100000;
Cheers! 干杯!
Remember that functions (and thus "object definitions") can be nested (there is absolutely no requirement that this is nested, but having it nested here allows a closure over start_value
): 请记住,函数(以及因此的“对象定义”)可以嵌套(绝对不需要嵌套此函数,但是将其嵌套在此处可以封闭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();
}
However, I would likely opt for this (just create a new "plain" Object): 但是,我可能会选择这样做(只需创建一个新的“普通”对象):
function userObject(start_value) {
this.name = start_value;
this.address = start_value;
this.cars = {
value: start_value,
count: start_value
};
}
Happy coding. 快乐的编码。
so simple anser is to use the object syntax 简单的分析方法是使用对象语法
function userObject(start_value) {
this.name = start_value;
this.address = start_value;
this.cars = {
value: start_value,
count: start_value
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.