Skip to content

Instantly share code, notes, and snippets.

@Williammer
Last active August 29, 2015 14:02
Show Gist options
  • Select an option

  • Save Williammer/4d5c31c1bff00cb7c56c to your computer and use it in GitHub Desktop.

Select an option

Save Williammer/4d5c31c1bff00cb7c56c to your computer and use it in GitHub Desktop.
javaScript.passValueTricks.js - Different condition when passing primitive value and object(and other reference types) to function.
function myfunction(b, context) {
// x is equal to 4
context.b = 5;
//alert(this);
return context;
//alert("x1: "+x);
// x is now equal to 5
}
var x = {
'b': 4
};
alert(x.b); // x is equal to 4
var xx = myfunction('b', x);
alert(xx.b); // x is 5
alert("x1: " + x.b); // x is 5 too
/////////// primitive value not work
// because they are immutable and are passed by value
function myfunction2(b) {
// x is equal to 4
b = 5;
}
var x2 = 4;
alert(x2); // x is equal to 4
myfunction2(x2);
alert(x2); //not changed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment