In Javascript how does the below code works.
var a = {
prop1: "a",
prop2: "b",
fun: function() {
return this.prop1 + " " + this.prop2;
}
}
var a2 = a;
a.fn = "v";
a = {};
if (a === a2) {
console.log(true);
} else {
console.log(false);
}
The above code prints false.
But if I comment out the line a={} the value which prints on console is true.
var a = {
prop1: "a",
prop2: "b",
fun: function() {
return this.prop1 + " " + this.prop2;
}
}
var a2 = a;
a.fn = "v";
//a={};
if (a === a2) {
console.log(true);
} else {
console.log(false);
}
How the above code works, as Both variables(a and a2) points to the same object but when I initialized a with {} it gave false.