-2

Maybe the question already exists, but I don't find anything because I don't know how to call this problem.

I have an object function and want to assign the object variable into a variable:

this.myFunction = function() {
    var selection;                // internal variable
    selection = this.selection;   // both are [0,0]

    console.log(this.selection);  // result: [0,0]

    selection[0] = 500;           // set internal variable[0] 500
    console.log(selection);       // result: [500,0]
    console.log(this.selection);  // result: [500,0] <-- expected: [0,0]
}

How I can change the local variable without changing the other?

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
Hendel Jo
  • 21
  • 3
  • 2
    You need to create a copy of the array. – Felix Kling Feb 13 '17 at 14:41
  • javascript assignment is always by reference for objects, if you want also the reason why – Kaddath Feb 13 '17 at 14:42
  • `Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.` This means whenever you assign an array to another variable, it's just a mutator. It's not a deep clone. – Sterling Archer Feb 13 '17 at 14:43
  • 4
    @Kaddath: that's a common misunderstanding and not what *by reference* means. JavaScript is *pass by value*, but the value is *a* reference (to an object). – Felix Kling Feb 13 '17 at 14:44
  • In JS, Objects (and arrays are objects) are passed by _reference_ -- as such, selection and this.selection _refer to the same array_. If you want them to be different you'll have to do as other commenters have suggested and make a copy of the array ([array.slice](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice) should be a good option to do that). – Alexander Nied Feb 13 '17 at 14:44
  • @anied: please see my previous comment. – Felix Kling Feb 13 '17 at 14:44
  • Not directly related to your question/problem, but what do you think `this` in `this.myFunction` means? What do you think `this` in `this.selection` means? –  Feb 13 '17 at 14:45
  • @SterlingArcher What do you mean by "mutator"? –  Feb 13 '17 at 14:45
  • good precisions Sterling Archer and Felix Kling, thx – Kaddath Feb 13 '17 at 14:45
  • @FelixKling - sure, fair point, this is discussed [here](http://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference#13104500). I was simply using a common shorthand to help illustrate the point. – Alexander Nied Feb 13 '17 at 14:47
  • @torazaburo it means that anything done to the array reflects on all references -- aka mutative. All native array methods mutate the array, they do not create clones. – Sterling Archer Feb 13 '17 at 15:20

1 Answers1

2

When you call selection = this.selection, you are copying the reference of this.selection value. So when you change the local variable selection, you are changing this.selection too.

You have to use the method slice() to create a new array from the value. Like this:

selection = this.selection.slice();
Community
  • 1
  • 1
Diego Faria
  • 8,805
  • 3
  • 26
  • 33