2

Possible Duplicate:
What is the most efficient way to clone a JavaScript object? (Most of these use external libraries, or don't work completely. The Blog Post below is a much better resource)

This is in the context of the scribd developer challenge. Their API allows me to call get_board and get the board. But, what I want to do is have a copy of that board that I modify in set_field. But, when I do that it messes with their own internal board object and breaks their code. So, What I need (I think) is to call get_board and then break the pointer to the object and have my own board I modify. I've attempted this below with mygame.board but I still get the same error.

The error reads: The type of an object is incompatible with the expected type of the parameter associated to the object

function new_game()
{
    var board = get_board();
    var mygame={board: board}
    mygame.board = set_field(min_max_store, mygame.board)
}

function set_field(min_max_store, board)
{

  //loop x
   //loop y
    board[x][y] = -1;
return board
}

How can I stop the pointer from pointing to their object and get my own to modify?

EDIT:

This will work for 2d arrays.

var one = [[1,2],[2,3],[3,4],[4,5]]
var two = one.slice(0)

for(var x = 0; x <= one.length - 1; x++)
{
  two[x] = one[x].slice(0)
}

one[1][1] = 10

console.log(one)
console.log(two)

returns:

[[1, 2], [2, 10], [3, 4], [4, 5]]
[[1, 2], [2, 3], [3, 4], [4, 5]]
Community
  • 1
  • 1
Noah Clark
  • 8,101
  • 14
  • 74
  • 116
  • 1
    There is no need to include the word *JavaScript* in the title .. thats what tags are for ... – Manse May 29 '12 at 15:14
  • @Quentin I'll look through those solutions some more, but I won't have access to any additional tools or APIs such as prototype or jquery. – Noah Clark May 29 '12 at 15:21

1 Answers1

3

When you copy the object as so, it will always create a pointer. But this is not true with primitive types.

You could use the method of looping through the object and taking all of its paramaters recursively (for depth). Another way to do it (sloppy, but it would work) is to encode the object into JSON, and then decode it back into the new variable.

Example:

var foo = 5;
// This is not a pointer, since it is a primitive type
var bar = foo;
// 'foo' will stay at 5
bar = 537;

var object_one = {
  var1: 3,
  var2: 5,
};

// This is just a pointer.
var object_two = object_one;

// This will change 'var1' of 'object_one' as well as 'object_two'
object_two.var1 = 5;

Example of using JSON:

var object_one = {
  var1: 'bar',
  var2: 5,
};
// A quick but dirty way to make a clone of the object without using a pointer 
var object_two = JSON.parse(JSON.stringify(object_one));

Example using recursion:

function clone_object(p_obj) {
  var new_obj = new Object;
  if (typeof(p_obj) == 'object') {
    for (var i in p_obj) {
      new_obj[i] = clone_object(p_obj[i]);
    }
    return new_obj;
  } else {
    return p_obj;
  }
}

var object_one = {
  var1: 'bar',
  var2: 5,
};
var object_two = clone_object(object_one); 

http://james.padolsey.com/javascript/deep-copying-of-objects-and-arrays/

elimirks
  • 1,452
  • 2
  • 17
  • 30
  • I've considered this. I just wondered if there was an easier way. Just so I understand I should able to loop through the object, extract the value and push it onto the new object right? – Noah Clark May 29 '12 at 15:20
  • Correct. var foo = 2; var bar = foo; bar = 377; alert(foo + ' ' + bar); – elimirks May 29 '12 at 15:22
  • I've used the blog post to create a solution above for 2d arrays. – Noah Clark May 29 '12 at 15:45
  • 1
    I added a little function that will copy infinitely deep objects if you want. I included it in the solution. – elimirks May 29 '12 at 15:55