Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #14197 > unrolled thread

'undefined' in declaration of function

Started bytrunikov <dmitry.trunikov@gmail.com>
First post2012-05-29 04:58 -0700
Last post2012-05-31 05:22 -0700
Articles 5 — 3 participants

Back to article view | Back to comp.lang.javascript


Contents

  'undefined' in declaration of function trunikov <dmitry.trunikov@gmail.com> - 2012-05-29 04:58 -0700
    Re: 'undefined' in declaration of function Scott Sauyet <scott.sauyet@gmail.com> - 2012-05-29 05:30 -0700
      Re: 'undefined' in declaration of function trunikov <dmitry.trunikov@gmail.com> - 2012-05-29 05:44 -0700
      Re: 'undefined' in declaration of function Dr J R Stockton <reply1222@merlyn.demon.co.uk.not.invalid> - 2012-05-30 20:06 +0100
        Re: 'undefined' in declaration of function Scott Sauyet <scott.sauyet@gmail.com> - 2012-05-31 05:22 -0700

#14197 — 'undefined' in declaration of function

Fromtrunikov <dmitry.trunikov@gmail.com>
Date2012-05-29 04:58 -0700
Subject'undefined' in declaration of function
Message-ID<adf008c0-39de-4eef-9dc3-d687899f6f7c@googlegroups.com>
Hi All :)

I've taken a look inside the jquery (core) library. I saw there strange function declaration:

(function( window, undefined ) {
  // bla bla bla
})();

Tell me please what is a goal to define last parameter as 'undefined'?
How does it work?

Thanks :)

[toc] | [next] | [standalone]


#14201

FromScott Sauyet <scott.sauyet@gmail.com>
Date2012-05-29 05:30 -0700
Message-ID<617c6390-5eea-43e2-881b-0fb60bb62e46@t35g2000yqd.googlegroups.com>
In reply to#14197
trunikov wrote:
> I've taken a look inside the jquery (core) library. I saw there strange function declaration:

First of all, you might see a lot of strange things there!  The jQuery
team goes through some interesting gymnastics to reduce code size,
sacrificing readability for brevity.  Unless you're already fairly
experienced, I would not suggest the jQuery core as a good model of
how to write your own code.  If you are, you might find some
interesting tricks in there.  But that said, this is a common idiom,
although I think you misquote it:

> (function( window, undefined ) {
>   // bla bla bla
> })();

I imagine that it actually reads:

| (function( window, undefined ) {
|   // bla bla bla
| })(this);
     ^^^^

The `this` parameter here is used so that the `window` parameter
inside the function is a reference to the global object.  In many
situations, it already is, but this technique enforces that.

> Tell me please what is a goal to define last parameter as 'undefined'?
> How does it work?

One silly flaw in Javascript that would probably have been removed had
the language not been standardized so quickly is that `undefined` is
not a keyword like `null`.  It is simply a global property.  Arbitrary
code can overwrite it.  So a library like jQuery that's operating in a
very heterogeneous environment cannot assume that no other code has
overwritten that property.  This is one way to restore it.  Inside the
function, `undefined` is now pointing to the value of the second
parameter passed to the function.  Since we don't pass a parameter
there, the value is undefined, exactly what we want the `undefined`
property to represent.  Note that this does *not* override the global
property; it simply shadows it.  Outside this function, everything
reverts to the way it was (except that any functions which close over
`undefined` will also have the shadowing version [and if that doesn't
make any sense to you, don't worry about it for now.])

There are other ways this could be done, but this one is common, and I
don't see any major flaws with it?  Other opinions?

  -- Scott

[toc] | [prev] | [next] | [standalone]


#14202

Fromtrunikov <dmitry.trunikov@gmail.com>
Date2012-05-29 05:44 -0700
Message-ID<6a019c94-199f-43ea-bdcb-43bda0245b18@googlegroups.com>
In reply to#14201
Hi Scott :)
Thank you very much for lucid explanation.
I didn't know heretofore that 'undefined' can be overridden/masked.
And you are absolutely right about error in the code snippet.
It should looks like this:

(function( window, undefined ) { 
   // bla bla bla 
})(window);

[toc] | [prev] | [next] | [standalone]


#14248

FromDr J R Stockton <reply1222@merlyn.demon.co.uk.not.invalid>
Date2012-05-30 20:06 +0100
Message-ID<+GbAIoKS$mxPFwJW@invalid.uk.co.demon.merlyn.invalid>
In reply to#14201
In comp.lang.javascript message <617c6390-5eea-43e2-881b-0fb60bb62e46@t3
5g2000yqd.googlegroups.com>, Tue, 29 May 2012 05:30:44, Scott Sauyet
<scott.sauyet@gmail.com> posted:

>One silly flaw in Javascript that would probably have been removed had
>the language not been standardized so quickly is that `undefined` is
>not a keyword like `null`.  It is simply a global property.  Arbitrary
>code can overwrite it.  So a library like jQuery that's operating in a
>very heterogeneous environment cannot assume that no other code has
>overwritten that property.  This is one way to restore it.  Inside the
>function, `undefined` is now pointing to the value of the second
>parameter passed to the function.  Since we don't pass a parameter
>there, the value is undefined, exactly what we want the `undefined`
>property to represent.  Note that this does *not* override the global
>property; it simply shadows it.  Outside this function, everything
>reverts to the way it was (except that any functions which close over
>`undefined` will also have the shadowing version [and if that doesn't
>make any sense to you, don't worry about it for now.])
>
>There are other ways this could be done, but this one is common, and I
>don't see any major flaws with it?  Other opinions?

The routine is then relying on something external to itself, its call.

I have used    var U    within the routine, with U on one side of  ==
or  !=  tests, with success;  possibly  ===  or  !==  tests would be
safer;  I don't generally use  U  for any other purpose when confusion
might occur.

-- 
 (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk  Turnpike v6.05  MIME.
  Web  <http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms and links;
  Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
 No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.

[toc] | [prev] | [next] | [standalone]


#14255

FromScott Sauyet <scott.sauyet@gmail.com>
Date2012-05-31 05:22 -0700
Message-ID<259fe765-af48-49c4-9115-b45552002bf4@h10g2000yqn.googlegroups.com>
In reply to#14248
Dr J R Stockton wrote:
> Scott Sauyet wrote:
>> [an explanation of the use of `undefined` in the following]
>>| (function(window, undefined) {
>>|   // ...
>>| }(this));

>> There are other ways this could be done, but this one is common, and I
>> don't see any major flaws with it?  Other opinions?
>
> The routine is then relying on something external to itself, its call.

Yes, it should only be used as described, when the function is
immediately executed. But that's not a significant limitation.  The
function and its invocation are inextricably bound together in such
blocks of code.

> I have used    var U    within the routine, with U on one side of  ==
> or  !=  tests, with success;  possibly  ===  or  !==  tests would be
> safer;  I don't generally use  U  for any other purpose when confusion
> might occur.

Yes this is another technique, although I usually prefer a variable
name such as `undef`.  Another I've seen is to assign it explicitly:

    var undef = void 0;

although I don't see that this gains anything over

    var undef;

AISB, there are many options.

  -- Scott

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web