Javascript reference reset tests
by Riffic
JavaScript
var firstVar = {
name: "JOE",
age: 252
};
//What will happen now if I load firstVar into MainObject
//and then set secondVar to null or change it completely?
MainObject = {
secondVar: null,
load: function(pass) {
this.secondVar = pass;
console.log("Main Object load ", pass);
},
modify: function(name) {
this.secondVar.name = name;
console.log("Main Object modify ", this.secondVar);
},
change: function(obj) {
this.secondVar = obj;
console.log("Main Object change", this.secondVar);
},
reset: function() {
this.secondVar = null;
console.log("Main Object reset second var", this.secondVar);
//Put secondVar back to firstVar for more testing..
this.secondVar = firstVar;
}
};
//Let's see
MainObject.load(firstVar);
MainObject.modify("JOHN");
//Showing firstVar to see the update by reference
console.log("Update by reference: ", firstVar);
//Now nullify to see what happens to reference
MainObject.reset();
//Note that by setting a referenced value to something different it does not set the reference,
//but reassigns the reference to something else;
console.log("Update by reference2: ", firstVar);
//Last test just to make sure
MainObject.change({Foo: 'bar', Changed: true});
console.log("Update by reference3: ", firstVar);