Take the following code...
window.thisPage = {
MyObject: function() {
//private var
var x = 1;
//private method
var inc = function() {
x = x+1;
};
//public method
this.output = function() {
return x;
};
inc(); //so x will always initially start out at 2
}
};
obj1 = new window.thisPage.MyObject();
obj2 = new window.thisPage.MyObject();
obj3 = new window.thisPage.MyObject();
obj4 = new window.thisPage.MyObject();
alert(obj2.output());
I understand now why window.
is proper when defining "thisPage" because that is explicit (based on this answer). But does that mean that I should continue to type window.
like in the area where I am creating the 4 objects? I know it will run either way. Just wondering what others think is proper and why. Or does it really not matter?
window
, just use a globalvar
declaration. – Bergi Oct 2 '14 at 19:35var
declaration are yourobj1
…obj4
variables – Bergi Oct 2 '14 at 19:35