JavaScript Nesting Objects

I have a question about JavaScript. I am currently using code similar to the code below:

function Game() { } 

I want to nest objects, so I can access them like this:

 var a = new Game(); a.nested_object.method(); a.nested_object.property; 

How can I do it? Am I using a function or {}? Or does it even matter? The code below is an example of the code I'm talking about.

 function Game() { this.id; var stats = {}; } 

As I said above, can I access the statistics as follows:

 var a = new Game(); a.stats 
+4
source share
6 answers

Yes, this is exactly what needs to be done.

Note that this keyword in your method() will contain nested_object , not your Game instance. You can only get a link to this with 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(); 

Due to the fact that nested objects on the prototype (where you do not have a variable containing an instance), it will rarely make sense - see Crockford's prototypical inheritance - Problems with nested objects .

+2
source

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:

  • Separation of problems - the game designer can completely concentrate on the game logic, and the statistics designer will focus only on the game statistics.
  • Modularity . You can put the game constructor and statistics constructor in two different files. This allows you to work with them separately and simplifies project management.
  • Loose Coupling . The statistics object should not know about the game object. So it’s better to separate it from the game object. If you create it using the object’s letter designation instead (as @Bergi did), then the statistics object has access to the private members of the game object (which can be counterproductive if the statistics object accidentally changes the private property of the game object).
  • Readability . Compare the @Bergi code and mine. The separation of statistics and the game object makes it easier to read and understand the code. You can take a look at the code and know exactly what is going on.
+5
source

Add nested material to this or Game.prototype .

0
source

[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 ); 
0
source

Just create a nested object in the constructor.

 function Game() { this.stats = { lives: 3 }; }; var a = new Game(); -- a.stats.lives; 

However, this can be annoying, since when implementing the Game you must refer to stats as this.stats . A reset of this ' es can occur when this refers to the wrong thing, for example, inside a function(){} expression.

My preferred template looks like this. This is essentially the classic OO gatekeeper feature.

 function Game() { var stats = { lives: 3 }; this.stats = function() { return stats; }; }; var a = new Game(); -- a.stats().lives; 
0
source

It 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 created object has no idea about the variable.

Check feed

0
source

All Articles