[英]Nesting JavaScript Objects
I am writing a simple JavaScript application. 我正在编写一个简单的JavaScript应用程序。 The code consists of creating an object
deck
which can have many nested card
objects. 该代码包括创建一个对象
deck
,其中可以包含许多嵌套的card
对象。
Basically, I wanted to be able to access each card in a deck instance like this: 基本上,我希望能够像这样在卡座实例中访问每个卡:
deck1.card2.method();
I have used the following function to create my deck prototype: 我使用以下函数来创建我的牌组原型:
function Deck(){
var cardStack = []
//deck properties & methods
}
function card(){
//card properties methods
}
I've been adding each card to the deck and storing them in the cardStack
array, using push
and pop
methods of JavaScript arrays. 我一直在使用JavaScript数组的
push
和pop
方法将每张卡添加到卡座中并将它们存储在cardStack
数组中。
This however does not allow me to access my cards as I wished to do: 但是,这不允许我按照自己的意愿来访问我的卡:
deck1.card2.method();
Can anyone point me in the right way to achieve this? 谁能指出我实现这一目标的正确方法? Is it possible in JavaScript?
JavaScript可能吗? Thank you in advance :-)
先感谢您 :-)
You need to define the objects and functions as properties of the parent object by using this
: 你需要通过定义对象和函数作为父对象的属性
this
:
function Deck() {
this.cardStack = [];
this.card = function() {
console.log('Hullo');
}
}
Now calling the method works: 现在调用该方法即可:
var deck = new Deck();
deck.card(); // Prints 'Hullo'
To access the single card objects, you will need to make your cardStack
a public property. 要访问单个卡对象,您需要将
cardStack
为公共属性。 Inside the constructor, the this
keyword is a reference to the current Deck
object, so you can use: 在构造函数内部,
this
关键字是对当前Deck
对象的引用,因此您可以使用:
function Deck(){
this.cards = [];
// deck properties & methods
}
Then, access a single card of var deck = new Deck(…)
via deck.cards[2]
, and call a method of it. 然后,通过
deck.cards[2]
访问一张var deck = new Deck(…)
deck.cards[2]
,并调用它的方法。
Or you use an accessor function to the cardStack
array. 或者,您可以使用
cardStack
数组的访问器函数。 Extended example: 扩展示例:
var Deck = (function() {
function Deck() {
var cardStack = [];
this.getCard = function(i) { return cardStack[i]; };
for (var i=0; i<5; i++)
cardStack.push(new Card(i));
}
function Card(i) {
this.method = function() {
alert("Hi, I'm card №"+i);
};
}
return Deck;
})();
var deck = new Deck();
deck.getCard(2).method(); // alerts "Hi, I'm card №2"
You can implement such a method dynamically simply by adding the property to your object at runtime in the method where you are pushing your card or deck onto the array. 您只需在将卡或卡座推入阵列的方法中,在运行时将属性添加到对象中,就可以动态地实现这种方法。
For example: 例如:
var DeckStack = {
initialize: function() {
this.deckStack = [];
},
addDeck: function (name) {
this.deckStack.push(name);
this['deck' + this.deckStack.length] = name; // <-- We add a method or property to the object dynamically
}
};
DeckStack.initialize();
DeckStack.addDeck('test');
console.log(DeckStack.deck1); // Writes 'test' to console
// This will show our new method/property as part of the object
for (var meth in DeckStack) {
console.log(meth);
}
Maybe you are creating a Card object. 也许您正在创建Card对象。 If so, you would use:
如果是这样,您将使用:
var card = new Card(name); // For example
this['deck' + this.deckStack.length] = card;
and it would be available as a property on your deck object. 它将作为甲板对象的属性提供。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.