I'm struggling with nested objects and prototyping. In the example below, I am creating 2 instances of object "o".
var o = function(){};
o.prototype = {
val : 1,
test : {
val2 : 1
}
};
var t1 = new o();
var t2 = new o();
t1.val = 5;
t2.val = 20;
t1.test.val2 = 5;
t2.test.val2 = 10;
console.log(t1.val) //5
console.log(t2.val) //20
console.log(t1.test.val2) //10
console.log(t2.test.val2) //10
I'm puzzled as to why t1.test.val2 === t2.test.val2, even though t1 and t2 are different variables. Shouldn't they be completely separate entities?
Any suggestions on how to modify the code so that all objects and variables remain distinct?