There is not a single time where even sesoned developers have not made this mistake some where in the game. Scratch their head long enough until blood oozes out, they relize the folly they had done and bang their head to the table saying "How did I do it?"
Before you go through the cyle long enough, it's good to understand how value-types and reference-types fit in JS world
Output
First String: Lorem ipsum Refernce: Lorem ipsum
First String: some other string Refernce: Lorem ipsum
Person object - FirstName:James, LastName: Bond
Reference object - FirstName:James, LastName: Bond
Person object - FirstName:Sheldon, LastName: Cooper
Reference object - FirstName:Sheldon, LastName: Cooper
Before you go through the cyle long enough, it's good to understand how value-types and reference-types fit in JS world
function valueTypesBehavior() {
var firstStringVar = "Lorem ipsum";
var firstStringRef = firstStringVar;
console.log('First String: ' + firstStringVar + '\nRefernce: ' + firstStringRef);
firstStringVar = "some other string";
console.log('First String: ' + firstStringVar + '\nRefernce: ' + firstStringRef);
};
function referenceTypesBehavior() {
var personVar = {
firstName: "James",
lastName: "Bond"
};
var personVarRef = personVar;
console.log('Person object - FirstName:' + personVar.firstName + ', LastName: ' + personVar.lastName);
console.log('Reference object - FirstName:' + personVarRef.firstName + ', LastName: ' + personVarRef.lastName);
personVar.firstName = "Sheldon";
personVar.lastName = "Cooper";
console.log('Person object - FirstName:' + personVar.firstName + ', LastName: ' + personVar.lastName);
console.log('Reference object - FirstName:' + personVarRef.firstName + ', LastName: ' + personVarRef.lastName);
};
(function() {
valueTypesBehavior();
referenceTypesBehavior();
})();
Output
First String: Lorem ipsum Refernce: Lorem ipsum
First String: some other string Refernce: Lorem ipsum
Person object - FirstName:James, LastName: Bond
Reference object - FirstName:James, LastName: Bond
Person object - FirstName:Sheldon, LastName: Cooper
Reference object - FirstName:Sheldon, LastName: Cooper
No comments:
Post a Comment