Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Need explanation for a Javascript snippet (http://www.velocityreviews.com/forums/t938475-need-explanation-for-a-javascript-snippet.html)

Vipul Kapadia 04-20-2009 11:46 AM

Need explanation for a Javascript snippet
 

Hi All,

Am pasting below a javascript which I need to comprehend. Please
help..

var foo = function()
{

return
{
func1:function(url)
{
... some javascript code ..
},
func2:function()
{
... some javascript code ...
},
func3:function()
{
.... some javascript code ...
}
};
}();

This is then called using:

foo.func2();
foo.func3();


It seems that it is creating some kind of class/object and then
calling the functions defined (nested ?), but I cant figure out where/
how these functions are getting initialized or what is exactly
happening.

It would be nice to get a explanation regarding this.

Thanks
Vipul Kapadia





Gregor Kofler 04-20-2009 02:30 PM

Re: Need explanation for a Javascript snippet
 
Vipul Kapadia meinte:
> Hi All,
>
> Am pasting below a javascript which I need to comprehend. Please
> help..
>
> var foo = function()
> {
>
> return
> {
> func1:function(url)
> {
> ... some javascript code ..
> },
> func2:function()
> {
> ... some javascript code ...
> },
> func3:function()
> {
> .... some javascript code ...
> }
> };
> }();
>
> This is then called using:
>
> foo.func2();
> foo.func3();
>
>
> It seems that it is creating some kind of class/object and then
> calling the functions defined (nested ?), but I cant figure out where/
> how these functions are getting initialized or what is exactly
> happening.
>
> It would be nice to get a explanation regarding this.


A function is invoked and the result assigned to foo. Since the return
value is an object with 3 methods, foo is now an object with three
methods. Easy, isn't it?

Gregor


--
http://www.gregorkofler.com
http://web.gregorkofler.com - vxJS, a JS lib in progress

Thomas 'PointedEars' Lahn 04-20-2009 02:46 PM

Re: Need explanation for a Javascript snippet
 
Gregor Kofler wrote:
> Vipul Kapadia meinte:
>> Am pasting below a javascript which I need to comprehend. Please
>> help..
>>
>> var foo = function()
>> {
>>
>> return
>> {
>> func1:function(url)
>> {
>> ... some javascript code ..
>> },
>> func2:function()
>> {
>> ... some javascript code ...
>> },
>> func3:function()
>> {
>> .... some javascript code ...
>> }
>> };
>> }();
>>
>> This is then called using:
>>
>> foo.func2();
>> foo.func3();
>>
>>
>> It seems that it is creating some kind of class/object and then
>> calling the functions defined (nested ?), but I cant figure out where/
>> how these functions are getting initialized or what is exactly
>> happening.
>>
>> It would be nice to get a explanation regarding this.

>
> A function is invoked and the result assigned to foo. Since the return
> value is an object with 3 methods, foo is now an object with three
> methods. Easy, isn't it?


That's only the loose explanation, of course. For example, `foo' is _not_
an object then, but *a reference to* the object that the returned *reference
refers to*.

Of course, all of that would have been easier to recognize if the OP
employed Pretty Printing and RTFM before posting.

<http://jibbering.com/faq/>


PointedEars

Gregor Kofler 04-20-2009 02:57 PM

Re: Need explanation for a Javascript snippet
 
Thomas 'PointedEars' Lahn meinte:

> That's only the loose explanation, of course. For example, `foo' is _not_
> an object then, but *a reference to* the object that the returned *reference
> refers to*.


Indeed.

Gregor


--
http://www.gregorkofler.com
http://web.gregorkofler.com - vxJS, a JS lib in progress

Vipul Kapadia 04-20-2009 05:02 PM

Re: Need explanation for a Javascript snippet
 
On Apr 20, 7:57*pm, Gregor Kofler <use...@gregorkofler.com> wrote:
> Thomas 'PointedEars' Lahn meinte:
>
> > That's only the loose explanation, of course. *For example, `foo' is _not_
> > an object then, but *a reference to* the object that the returned *reference
> > refers to*.

>
> Indeed.
>
> Gregor
>
> --http://www.gregorkofler.comhttp://web.gregorkofler.com- vxJS, a JS lib in progress


Thanks for your replies. I had one more question.

Isn't this cryptic kind of code ? Would creating a regular object with
functions and calling those functions have the same effect as this ?

Are there any advantages of writing code in this manner ?

Thanks
Vipul Kapadia


Thomas 'PointedEars' Lahn 04-20-2009 05:43 PM

Re: Need explanation for a Javascript snippet
 
Vipul Kapadia wrote:
> Gregor Kofler wrote:
>> Thomas 'PointedEars' Lahn meinte:
>>> That's only the loose explanation, of course. For example, `foo' is _not_
>>> an object then, but *a reference to* the object that the returned *reference
>>> refers to*.

>> Indeed.
>> [snip sig]


Trim your quotes to the relevant minimum, please:

<http://jibbering.com/faq/#posting>

(You had been pointed to the FAQ already.)

> [...]
> Isn't this cryptic kind of code ? Would creating a regular object with
> functions and calling those functions have the same effect as this ?


Yes and yes:

var foo = {
func1: function(url) {
/* some javascript code */
},

func2: function() {
/* some javascript code */
},

func3: function() {
/* some javascript code */
}
};

foo.func2();
foo.func3();

(Note how Pretty Printing helps to see what's going on.)

There are no classes, BTW.

> Are there any advantages of writing code in this manner ?


Apparently not here.


PointedEars

Gregor Kofler 04-20-2009 05:49 PM

Re: Need explanation for a Javascript snippet
 
Vipul Kapadia meinte:

>> --http://www.gregorkofler.comhttp://web.gregorkofler.com- vxJS, a JS lib in progress


No need to quote signatures...

> Thanks for your replies. I had one more question.
>
> Isn't this cryptic kind of code ? Would creating a regular object with
> functions and calling those functions have the same effect as this ?


Depends. And depends on what you mean by "regular" object.

> Are there any advantages of writing code in this manner ?


The posted code allows privacy [1]. Invocation as function instead of
constructor. Maybe be more - depends on your definition of "regular object".

Gregor

[1]
http://www.crockford.com/javascript/private.html

--
http://www.gregorkofler.com
http://web.gregorkofler.com - vxJS, a JS lib in progress


All times are GMT. The time now is 01:21 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.