Skip to main content
added 42 characters in body
Source Link
nnnnnn
  • 150.4k
  • 30
  • 210
  • 247

I don't see anything wrong with your .clone() method.

The problem is in your constructor, in how it sets up the .getBoard() method. Each time your constructor is called you overwrite the prototype .getBoard() method, and that method refers to a local variable that is captured by the closure, not an instance variable. So call .getBoard() for any instance of SudokuBoard and you get the prototype method that will reference the local variable from the latest instance's closure and thus only return the most recently created board.

Change this line:

SudokuBoard.prototype.getBoard = function() 

to create an instance method instead of a prototype method:

this.getBoard = function() 

If every instance has its own .getBoard() they'll all reference their own closure.

I don't see anything wrong with your .clone() method.

The problem is in your constructor. Each time your constructor is called you overwrite the prototype .getBoard() method, and that method refers to a local variable that is captured by the closure, not an instance variable. So call .getBoard() for any instance of SudokuBoard and you get the prototype method that will only return the most recently created board.

Change this line:

SudokuBoard.prototype.getBoard = function() 

to create an instance method instead of a prototype method:

this.getBoard = function() 

I don't see anything wrong with your .clone() method.

The problem is in your constructor, in how it sets up the .getBoard() method. Each time your constructor is called you overwrite the prototype .getBoard() method, and that method refers to a local variable that is captured by the closure, not an instance variable. So call .getBoard() for any instance of SudokuBoard and you get the prototype method that will reference the local variable from the latest instance's closure and thus only return the most recently created board.

Change this line:

SudokuBoard.prototype.getBoard = function() 

to create an instance method instead of a prototype method:

this.getBoard = function() 

If every instance has its own .getBoard() they'll all reference their own closure.

Source Link
nnnnnn
  • 150.4k
  • 30
  • 210
  • 247

I don't see anything wrong with your .clone() method.

The problem is in your constructor. Each time your constructor is called you overwrite the prototype .getBoard() method, and that method refers to a local variable that is captured by the closure, not an instance variable. So call .getBoard() for any instance of SudokuBoard and you get the prototype method that will only return the most recently created board.

Change this line:

SudokuBoard.prototype.getBoard = function() 

to create an instance method instead of a prototype method:

this.getBoard = function()