如何在对象内创建 JavaScript 对象?
例如:
function userObject(start_value) {
this.name = start_value;
this.address = start_value;
this.cars = function() {
this.value = start_value;
this.count = start_value;
};
}
显然上述内容不起作用,但希望了解如何让汽车可用: userObject.cars.value = 100000;
干杯!
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;
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,函数(以及“对象定义”)可以嵌套(绝对不要求嵌套,但将其嵌套在此处允许对
start_value
进行闭包):但是,我可能会选择为此(只需创建一个新的“普通”对象):
快乐编码。
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
):However, I would likely opt for this (just create a new "plain" Object):
Happy coding.
如此简单的 anser 就是使用对象语法
so simple anser is to use the object syntax