17125

Javascript - [Why?] Assigning a variable to an object [duplicate]

Question:

This question already has an answer here:

<ul><li> <a href="/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language" dir="ltr" rel="nofollow">Is JavaScript a pass-by-reference or pass-by-value language?</a> <span class="question-originals-answer-count"> 29 answers </span> </li> </ul>

Why is it that when I assign a variable to an object and make a change to that variable it also changed the objects? For example:

c = 26; a = b = c; a += 1; a // 27 b // 26 c // 26

but

z = {}; x = y = z; x.ab = 5; x // Object {ab: 5} y // Object {ab: 5} z // Object {ab: 5}

Why (in the example above) does y.ab and z.ab exist? I only modified x not y or z. Howcome in the first example (with the integers), when I changed the value of a, b and c weren't affected?

Answer1:

When you assign an object to a variable, it just makes a reference to the original object, it doesn't make a copy. So all the variables refer to the same object.

Recommend

  • Mocha throws unexpected token error for ES6 object spread operator
  • Criterion causing memory consumption to explode, no CAFs in sight
  • how to set variables in a php include file?
  • Authentication in Play! and RestEasy
  • How do I configure context broker accept post requests from my remote sensor?
  • What's the purpose of QString?
  • Assign variable to the value in HTML
  • Blackberry - Custom EditField Cursor
  • How to define and use opencv mat of user type
  • Content-Length header not returned from Pylons response
  • Extracting HTML between tags
  • Ajax Loaded meta Tags
  • Xamarin Forms - UWP Fonts
  • Spring Data JPA custom method causing PropertyReferenceException
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • HTML download movie download link
  • Excel - Autoshape get it's name from cell (value)
  • Updating server-side rendering client-side
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • vba code to select only visible cells in specific column except heading
  • Arrow is showed instead of the material design version hamburger icon. Why doesn't syncState in
  • Redux, normalised entities and lodash merge
  • Weird JavaScript statement, what does it mean?
  • How do you troubleshoot character encoding problems?
  • Jquery - Jquery Wysiwyg return html as a string
  • Calling of Constructors in a Java
  • AT Commands to Send SMS not working in Windows 8.1
  • Arrays break string types in Julia
  • Traverse Array and Display in markup
  • How to format a variable of double type
  • Transpose CSV data with awk (pivot transformation)
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • WPF Applying a trigger on binding failure
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How to set the response of a form post action to a iframe source?
  • How do I configure my settings file to work with unit tests?
  • Java static initializers and reflection
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Is it possible to post an object from jquery to bottle.py?
  • How to Embed XSL into XML