Forum Moderators: open

Message Too Old, No Replies

Please help me understand object notation scope.

         

MarcMiller

2:51 am on Oct 11, 2008 (gmt 0)

10+ Year Member



Hi
I had been reading about object notation and it appears you can to clear what is global variables to your object in a manner like this "a : 9,", however this does not work for me in my simple test program below. So could you please be so good as to correct my work and explain your corrections.

ObjectNotation = {
a : 9,
b : 6,
add : function(){
var c = a + b;
alert(c);
}
}
window.addEventListener("load", function(e) {ObjectNotation.add(); }, false);

Little_G

12:28 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



Hi,

Changes:

var c = this.a + this.b; 

a and b are both properties of the object so are accessed using the this keyword. Read more here,
[developer.mozilla.org...]

Andrew

MarcMiller

2:09 pm on Oct 11, 2008 (gmt 0)

10+ Year Member



Yes it works exactly the way stated. Thanks.