Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > () after function call

Reply
Thread Tools

() after function call

 
 
Brownie Guy
Guest
Posts: n/a
 
      02-06-2008
Today while researching a bug I found this script:

var Event = {
add: function() {
if (window.attachEvent) {
return function(el, type, fn) {
var f = function() { fn.call(Dom.get(el), window.event); };
Dom.get(el).attachEvent('on' + type, f);
};
}
}()
};

Its the () at the end of the function definition that leaves me
scratching my head. I looked around but I wasn't able to learn the
name of this feature and thus unable to research it farther. Can
someone point me in the right direction or explain it.

Much Appreciated
Travis
 
Reply With Quote
 
 
 
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-06-2008
Brownie Guy <(E-Mail Removed)> writes:

> Today while researching a bug I found this script:
>
> var Event = {
> add: function() {
> if (window.attachEvent) {
> return function(el, type, fn) {
> var f = function() { fn.call(Dom.get(el), window.event); };
> Dom.get(el).attachEvent('on' + type, f);
> };
> }
> }()
> };
>
> Its the () at the end of the function definition that leaves me
> scratching my head. I looked around but I wasn't able to learn the
> name of this feature and thus unable to research it farther. Can
> someone point me in the right direction or explain it.


Basically, it's just a function call on the anynonymous function.

I.e:

a = {
b: function() { return "result" }()
};

alert(a.b); // note: a.b. is a string, not a function

Note that in some cases you need additional parentheses to make it work:

function(){ alert("call" }(); // error

(function(){ alert("call"} )(); // works

Joost.






 
Reply With Quote
 
 
 
 
Brownie Guy
Guest
Posts: n/a
 
      02-06-2008
On Feb 6, 2:42 pm, Joost Diepenmaat <(E-Mail Removed)> wrote:
> Brownie Guy <(E-Mail Removed)> writes:
> > Today while researching a bug I found this script:

>
> > var Event = {
> > add: function() {
> > if (window.attachEvent) {
> > return function(el, type, fn) {
> > var f = function() { fn.call(Dom.get(el), window.event); };
> > Dom.get(el).attachEvent('on' + type, f);
> > };
> > }
> > }()
> > };

>
> > Its the () at the end of the function definition that leaves me
> > scratching my head. I looked around but I wasn't able to learn the
> > name of this feature and thus unable to research it farther. Can
> > someone point me in the right direction or explain it.

>
> Basically, it's just a function call on the anynonymous function.
>
> I.e:
>
> a = {
> b: function() { return "result" }()
>
> };
>
> alert(a.b); // note: a.b. is a string, not a function
>
> Note that in some cases you need additional parentheses to make it work:
>
> function(){ alert("call" }(); // error
>
> (function(){ alert("call"} )(); // works
>
> Joost.


Thanks Joost.

Now I understand the () after the definition calls the function that
was just defined immediately.

During what circumstances would you wrap the def in () as your second
example demonstrates?

Is this supported by all browsers?

Travis
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      02-06-2008
Brownie Guy <(E-Mail Removed)> writes:

> Thanks Joost.
>
> Now I understand the () after the definition calls the function that
> was just defined immediately.
>
> During what circumstances would you wrap the def in () as your second
> example demonstrates?


When I use this construct I always wrap it in extra parentheses, because
I simply don't want to think about when I do or don't need to. One case
where you need to wrap it is if you don't assign the result (which is
useful, because it creates a new scope):

(function() {
var temp = a;
function Something() {
return a;
}
function Inc() {
a++;
}
})();

// temp is not available here, so it won't overwrite anything

> Is this supported by all browsers?


As far as I know it should work in everything. I know for sure it works
in fairly recent versions of opera, mozilla, safari and IE. I'm not
aware of any problems in older versions / other browsers.

Joost.
 
Reply With Quote
 
Brownie Guy
Guest
Posts: n/a
 
      02-06-2008
On Feb 6, 3:07 pm, Joost Diepenmaat <(E-Mail Removed)> wrote:
> Brownie Guy <(E-Mail Removed)> writes:
> > Thanks Joost.

>
> > Now I understand the () after the definition calls the function that
> > was just defined immediately.

>
> > During what circumstances would you wrap the def in () as your second
> > example demonstrates?

>
> When I use this construct I always wrap it in extra parentheses, because
> I simply don't want to think about when I do or don't need to. One case
> where you need to wrap it is if you don't assign the result (which is
> useful, because it creates a new scope):
>
> (function() {
> var temp = a;
> function Something() {
> return a;
> }
> function Inc() {
> a++;
> }
>
> })();
>
> // temp is not available here, so it won't overwrite anything
>
> > Is this supported by all browsers?

>
> As far as I know it should work in everything. I know for sure it works
> in fairly recent versions of opera, mozilla, safari and IE. I'm not
> aware of any problems in older versions / other browsers.
>
> Joost.


Thanks, thats all the info I need.

Travis
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      02-10-2008
Joost Diepenmaat wrote:
> [...]
> (function() {
> var temp = a;
> function Something() {
> return a;
> }
> function Inc() {
> a++;
> }
> })();
>
> // temp is not available here, so it won't overwrite anything
>
>> Is this supported by all browsers?

>
> As far as I know it should work in everything. I know for sure it works
> in fairly recent versions of opera, mozilla, safari and IE. I'm not
> aware of any problems in older versions / other browsers.


It is supported where FunctionExpressions are supported, meaning that if

var o = {
x: function()
{
return 42;
}
};

is supported, it is safe to write

var o = {
x: (function()
{
return 42;
})()
};

http://PointedEars.de/es-matrix#f


PointedEars
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 4 01-30-2008 05:12 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 0 01-30-2008 04:19 AM
defined? for recursive function call v/s defined? for function call stack Alok Ruby 3 04-13-2006 11:53 AM
write a function such that when ever i call this function in some other function .it should give me tha data type and value of calling function parameter komal C++ 6 01-25-2005 11:13 AM



Advertisments