PDA

View Full Version : function/ inheritence



mmdullah
Oct 16th, 2010, 09:16 AM
I wonder if there is difference between the following two:

1.
var obj1= {
a:0,
b=function(y){ window.alert(y);},
c:2;
}

function call: obj1.b('hello world');

and
2.
var obj1= {
a:0,
b:function(y){ window.alert(y);},
c:2;
}

function call: obj1.b('hello world');


In short , I want to know what is the diff between

foo:function(bar){window.alert(bar);}
and var foo=function(bar){window.alert(bar);}

Zefris
Oct 16th, 2010, 02:13 PM
#1, you're assigning the function to a key in a JSON object

#2, you're assigning a function to a variable

Btw, #1 wouldn't work since a key's expected on "b"... and "c" on both shouldn't have ";"...


foo:function(bar){window.alert(bar);}

Above would have to be accessed by <obj>.foo("stuffs"); where <obj> is the object you set "foo" on... while...


var foo=function(bar){window.alert(bar);}

would can be accessed by foo("stuffs");