[英]JavaScript nesting objects
I have a question about JavaScript. 我有一个关于JavaScript的问题。 I'm currently using code similar to the code below:
我目前正在使用类似于以下代码的代码:
function Game() {
}
I want to nest objects, so I can access them like so: 我想嵌套对象,所以我可以像这样访问它们:
var a = new Game();
a.nested_object.method();
a.nested_object.property;
How would I go about doing this? 我该怎么做呢? Would I use a function or {}?
我会使用函数还是{}? Or does it even matter?
或者甚至重要吗? The code below is an example code of what I am referring to.
下面的代码是我所指的示例代码。
function Game() {
this.id;
var stats = {};
}
Like I've stated above, can I access stats like so: 就像我上面所说的那样,我可以像这样访问统计数据:
var a = new Game();
a.stats
I would do this: 我会这样做:
function Game() {
this.id;
this.stats = new Stats(this);
}
function Stats(game) {
this.property;
this.method = method;
function method() {
this.property;
game.id;
}
}
var game = new Game;
game.stats.method();
The reasons are as follows: 原因如下:
Yes, that's exactly the way to go. 是的,这正是要走的路。
Notice that the this
keyword in your method()
will hold the nested_object
, not your Game
instance. 请注意,
method()
中的this
关键字将包含nested_object
,而不是您的Game
实例。 You can get a reference to that only by using a variable pointing to: 只能通过使用指向以下变量的变量来获取对它的引用:
function Game() {
var that = this; // the Game instance
this.id = …;
this.nested_object = {
property: "nested!",
method: function() {
this.property; // nested! (=== that.nested_object.property)
that.id // the game property
}
};
}
var game = new Game;
game.nested_object.method();
Because of that nested objects on the prototype (where you don't have a variable containing the instance) will seldom make much sense - see Crockford's Prototypal inheritance - Issues with nested objects . 由于原型上的嵌套对象(你没有包含实例的变量)很少有意义 - 请参阅Crockford的Prototypal继承 - 嵌套对象的问题 。
将“嵌套”内容添加this
或Game.prototype
。
[Edited to respond to comments below] [编辑回复以下评论]
How about this: 这个怎么样:
function Game() {
this.nested_object = {
method: function () {
return 'method return value';
},
property: 'property value'
};
};
var a = new Game();
alert( a.nested_object.method() );
alert( a.nested_object.property );
Just create the nested object in the constructor. 只需在构造函数中创建嵌套对象。
function Game() {
this.stats = { lives: 3 };
};
var a = new Game();
-- a.stats.lives;
However, this can be annoying as in the implementation of Game
you must refer to stats
as this.stats
. 但是,这可能很烦人,因为在
Game
的实现中你必须将stats
称为this.stats
。 The this
'es add up and confusion can arise when this
refers to the wrong thing, for example inside a function(){}
expression. 在
this
“ES加起来时可能出现的混淆this
指的是错误的事情,例如一内部function(){}
表达。
My preferred pattern looks like this. 我喜欢的模式看起来像这样。 It's essentially a classic OO getter function.
它本质上是一个经典的OO getter功能。
function Game() {
var stats = { lives: 3 };
this.stats = function() { return stats; };
};
var a = new Game();
-- a.stats().lives;
This should be more appropriate 这应该更合适
function Game() {
this.id;
this.stats = "Hello";
return this;
}
var a = new Game();
alert(a.stats);
Basically in your case stats is a local variable
, and the object created has no idea about the variable. 基本上在你的情况下,
stats is a local variable
,创建的对象不知道变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.