Anonymous function

R

Ryan Chan

Hello,

I knwe function in JS can be written like below to avoid function name
collision, e.g.

<script>

(function(i, j) {
alert(i + j);
}) (1, 2);

</script>

However, beside avoiding function name collision, are there any other
benefits / drawbacks, e.g.
memory usage, speed?
 
L

Lasse Reichstein Nielsen

Ryan Chan said:
I knwe function in JS can be written like below to avoid function name
collision, e.g.
(function(i, j) {
alert(i + j);
}) (1, 2);
However, beside avoiding function name collision, are there any other
benefits / drawbacks, e.g.
memory usage, speed?

Memory usage:
Maybe. It's possible that the code for the body of the function can
be garbage collected after it has been run, while the top level code for
a script element might be retained ... unless the code for the script
element will be GC'ed too.
By avoiding global variable names, it's likely that some data might be
GC'ed after the function has returned that would otherwise be retained
... unless you null or delete the variables manually.

Speed:
Mayble. You create a function object and call it, so that's a slight
overhead. Inside the function, local variable lookup might be faster
than global variable lookup (for several reasons), but if you do need
global variable lookup inside the function, some implementations might
be marginally slower (but a good optimizing implementation should be
able to determine whether a lookup is global and not need to traverse
the scope chain at runtime).
In any case, the code is likely to be run only once, so unless it
contains a big loop, performance is not going to be measurable.

The big win is maintainance. Avoiding putting variables into the
global object makes collisions less likely (and possibly other
global object lookups marginally faster).

/L
 
J

Jorge

(...)
1.  Named functions are hoisted.  The following works!
    // Top level
     wibble(3)
     function wibble(n){ alert(n) };
(...)

Hoisting has nothing to do with naming. FunctionDeclarations are
hoisted, FunctionExpressions are not... except in IE due to yet
another of its hundreds of long standing, carefully chosen
misbehaviors (intentional bugs) that the biggest software company in
the world is still purposely unwilling to fix after over a decade...
for the bad of the web.

This works in IE, and nowhere else:

<script>
f();
var e= function f () {alert('Hello world.')};
</script>
 
J

Jorge

I think we are at cross-purposes here.  If you never write
      function name(args){ ... };
you'll never write anything that can be hoisted.




This is, I think, another reason for never writing
      function name(args){ ... };

No, this is, I think, another reason why not to use IEs is the right
thing to do: IEs are broken.
 
J

Jorge

(...)
My habit is always to write
     var name = function (args) { ... };
(...)

While this will fail:

<script>

var f;

(f= function () {
alert("3 seconds more");
setTimeout(f, 3000);
})();

f= null;

</script>

This won't:

<script>

(function f () {
alert("3 seconds more");
setTimeout(f, 3000);
})();

f= null;

</script>

Because the function's name is declared in its own scope, not in the
enclosing one (and therefore exposed).
 
O

optimistx

Jorge said:
No, this is, I think, another reason why not to use IEs is the right
thing to do: IEs are broken.

Not use IE yourself? Or not writing code, which your customers are
using with IE?

If I were selling something, I could not afford to indirectly tell to my
potential
customers: 'Because you are using IE, I do not sell to you'.

So I cannot imagine how to ignore IE.
 

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

Forum statistics

Threads
474,089
Messages
2,570,602
Members
47,222
Latest member
jspanther

Latest Threads

Top