Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

when I pass a variable as a function parameter, is a new copy of the variable value or a memory reference javascript

function changeObject(x) {
  x = { member: "bar" };
  console.log("in changeObject: " + x.member);
}

function changeMember(x) {
  x.member = "bar";
  console.log("in changeMember: " + x.member);
}

var x = { member: "foo" };

console.log("before changeObject: " + x.member);
changeObject(x);
console.log("after changeObject: " + x.member); /* change did not persist */

console.log("before changeMember: " + x.member);
changeMember(x);
console.log("after changeMember: " + x.member); /* change persists */
 Run code snippetHide results
Comment

PREVIOUS NEXT
Code Example
Javascript :: js does array.map maintain the order 
Javascript :: TypeError: this.jsonEnabled is not a function 
Javascript :: switch browser to fullscreen 
Javascript :: vue electron min width 
Javascript :: adonisjs hook befor save 
Javascript :: codewars js Beginner Series #1 School Paperwork 
Javascript :: string to accept two characters after point javascript 
Javascript :: electron hot model reload 
Javascript :: add disabled js 
Javascript :: javascript toggle variable 
Javascript :: logbox ignore warnings 
Javascript :: angular input date binding on variable update 
Javascript :: javascript button onclick 
Javascript :: javascript object to params string 
Javascript :: Javascript remove array item by value 
Javascript :: react native gradient touchable feedback 
Javascript :: 11.10*15.1667 
Javascript :: sendgrid bulk hide each other on the email 
Javascript :: how to shuffle an array in js 
Javascript :: remove tr having delete icon inside jquery 
Javascript :: array reduce and count based on proeperty js 
Javascript :: make created element brighter 
Javascript :: onclick focus out jquery 
Javascript :: nodejs redis json count object keys 
Javascript :: how to convert char to number in js 
Javascript :: axios not defined 
Javascript :: javascript match number 
Javascript :: jquery get all file input elements 
Javascript :: REPLACE BROKEN IMAGES WITH A DEFAULT IMAGE 
Javascript :: js remove element by tagname 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =