Is it correct to call the constructor of a JavaScript object in one of the prototypes?

I am reviewing JavaScript code from a colleague, and he wrote an object prototype function, which I believe is incorrect regarding the principles of object-oriented programming.

Game.prototype.reset = function() { if (game.over) { game.over = false; game = new Game(players); } } 

A few lines below, the game variable has been declared a global variable.

 var game = new Game(players); 

So, is it right to create a new Game object from one of its constructors? The code works great. Thanks!

+5
source share
4 answers

It should not refer to the game variable inside the prototype method, since game is the name of the instance. Instead, it should use this to indicate the current object.

Inside the reset method, it should not create a new instance of the game, but really reset the game’s fields, something below:

 Game.prototype.reset = function() { if (this.over) { this.over = false; this.initialize(this.players); } } Game.prototype.initialize = function(players) { this.players = players; : } 
+4
source

I am a guest, this has broken the principle of encapsulation, each instance is responsible for its behavior, and not with another global instance.

http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)

If I were, I would choose the Max Zoom option to simulate my solution.

+1
source

I would say that this is β€œwrong” code, because it does not obey the principle of encapsulation, because one instance of a class affects the state of another instance of this class, without this second instance being passed as a parameter (it just goes out of nowhere).

I would also say that it is not particularly useful to define parts of a module between a constructor and a single.

If the game should be single-point, then define it as a singleton, attach it to the global scope (or, better, use the module loader) and use it as a singleton. Give it a reset method that uses this to change its internal state to its initial values.

If game should be a constructor from which there can be many instances, then define it as a constructor and create instances if necessary, and then pass these instances around as parameters where necessary.

Personally, I would use a constructor, and I would not have a reset method at all. When the game is reset by the user, I simply create a new instance of the game . But a well-defined singleton will be better than the code in question.

+1
source

I don’t think there is anything wrong with using the Game constructor inside the Game prototype.

See the example below. it has a method called cloneMe and creates a new object with the current value of the object.

 function Game(name, team) { this.playerName = name; this.teamName = team this.points = 0; } Game.prototype.goal = function() { this.points += 1; } Game.prototype.cloneMe = function() { return new Game(this.playerName, this.teamName); } var gameInst = new Game('jack', 'X-team'); gameInst.goal(); gameInst.points // will print 1 gameInst.goal(); gameInst.points // will print 2 // game varible got replaced with new variable var gameCloneInst = game.cloneMe(); gameCloneInst.goal(); gameCloneInst.points // will print 1 
0
source

All Articles