By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,608 Members | 1,940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,608 IT Pros & Developers. It's quick & easy.

OO question with anonymous functions

P: n/a
ed
Hi,

I was looking at the source of the following:

http://www.openjsan.org/doc/g/gu/gug.../TagCloud.html

I don't understand the point of using

Widget.TagCloud.prototype = (function() return { **object literal
functions** })(this.data)

I (think...to some degree) I understand that:
- the return statement is returning a object literal
- the anonymous function is a closure used to make the "this"
statements in the literal object refer to the correct execution
context

What I don't understand is:
- why there are parenthesis around the code preceding the parameter
list *i.e. =(function() return... }) *, and what it does
- what advantages there are to using the anonymous function in the
first place... why not just set the prototype to the object literal
directly? I am guessing there are some encapsulation reasons, but I
am not sure what.

Any help?

-e

Jun 28 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
d d
ed wrote:
I don't understand the point of using
Widget.TagCloud.prototype = (function() return { **object literal
functions** })(this.data)
- what advantages there are to using the anonymous function in the
first place... why not just set the prototype to the object literal
directly? I am guessing there are some encapsulation reasons, but I
am not sure what.
I think people just like showing off that they can make
code that's difficult to understand. Maybe job security?

~dd
Jun 28 '07 #2

P: n/a
d d wrote:
ed wrote:
>I don't understand the point of using
Widget.TagCloud.prototype = (function() return { **object literal
functions** })(this.data)
- what advantages there are to using the anonymous function in the
first place... why not just set the prototype to the object literal
directly? I am guessing there are some encapsulation reasons, but I
am not sure what.

I think people just like showing off that they can make
code that's difficult to understand. Maybe job security?
What is so difficult to understand about function expressions?

http://developer.mozilla.org/en/docs...on_operator.29

Also, see these 2 posts by RobG:

http://groups.google.com/group/comp....e1a902ffd2b059
http://groups.google.com/group/comp....747801812ebfc2

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Jun 30 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.