Consider this example:
If you were to take the above code and run it, the console message would be 100. Why does it print out 100 when I added the 100 to the position in obj2's "this"? How do I make it so that obj2 has its own unique "this"? Also please know that is an simplified example of my code, I cannot just make a seperate object in obj2 which contains everything.
var obj1 = function() {
this.obj2Arr = [];
this.pos = {
"x": 0,
"y": 0
};
this.obj2Arr.push(new obj2(this.pos));
console.log(this.pos.x);
// Prints out 100 even though obj2 was where I added the 100 for it's "this".
// Why does it do that, and how do I make obj2's "this" unique to it?
};
var obj2 = function(pos) {
this.pos = pos;
this.pos.x = this.pos.x + 100;
};
var example = new obj1();
console.log(example);