Skip to content

Instantly share code, notes, and snippets.

@awilson28
Last active August 29, 2015 14:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save awilson28/60ab45fc882906371867 to your computer and use it in GitHub Desktop.
Save awilson28/60ab45fc882906371867 to your computer and use it in GitHub Desktop.
//this function is a 'side-effects' function: it doesn't return anything, it just changes the values passed in
function changeStuff(num, obj1, obj2){
//remember that parameters are implicit variable declarations; these three parameters
//are variables local to this function
num = num * 10;
obj1.item = "changed";
obj2 = {item: "changed"};
}
var num = 10;
var obj1 = {item: "unchanged"};
var obj2 = {item: "unchanged"};
changeStuff(num, obj1, obj2);
console.log(num); // 10
console.log(obj1.item); // 'changed'
console.log(obj2.item); // 'unchanged'
//EXAMPLE 2:
function f(a,b,c) {
a = 3;
b.push("foo");
c.first = false;
}
var x = 4;
var y = ["eeny", "miny", "mo"];
var z = {first: true};
f(x,y,z);
console.log(x) // 4
console.log(y) // ["eeny", "miny", "mo", "foo"]
console.log(z) // {first: false}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment