Need explanation for a Javascript snippet

V

Vipul Kapadia

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
 
G

Gregor Kofler

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
 
T

Thomas 'PointedEars' Lahn

Gregor said:
Vipul Kapadia meinte:

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
 
G

Gregor Kofler

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
 
V

Vipul Kapadia

Thomas 'PointedEars' Lahn meinte:


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
 
T

Thomas 'PointedEars' Lahn

Vipul said:
Gregor said:
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
 
G

Gregor Kofler

Vipul Kapadia meinte:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top