Posts

Showing posts with the label pass-by-value

In javascript how does the below code works

In javascript how does the below code works 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. In the first example a points to the assigned {} and a2 to the object a hold before {} was assigned to it. var a2=a; does not create an alias or r...