After posting about assigning objects in Python only copies the reference and does not clone the object, I wanted to verify if the behavior happened in Javascript. Here is the code:
-
var myClass = function() {
-
this.text = '';
-
this.say = function() {
-
console.log(this.text);
-
}
-
}
-
var A = new myClass();
-
var B = new myClass();
-
A.text = 'testing A';
-
B.text = 'texting B';
-
A.say();
-
B.say();
-
A = B;
-
A.say();
-
B.say();
-
A.text = 'testing A2';
-
A.say();
-
B.say();
Here is the output:
testing A texting B texting B texting B testing A2 testing A2
Both A and B end up pointing to the same instance in memory, while the old A instance is inaccessible.
Demo: http://code.enekoalonso.com/js/tests/copy-constructor-test.html