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


Groups > comp.lang.javascript > #22475

Re: Wierd use of "new function() {}"

Path csiph.com!usenet.pasdenom.info!news.albasani.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="UTF-8"
Message-ID <2053658.P1axspRLhA@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Sun, 29 Dec 2013 17:45:05 +0100
User-Agent KNode/4.11.3
Content-Transfer-Encoding 8Bit
X-Face %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v
Face iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg==
Subject Re: Wierd use of "new function() {}"
Newsgroups comp.lang.javascript
References <ROqdnRynNfBWDCLPnZ2dnUVZ5u2dnZ2d@giganews.com> <Ks-dnQAIxZaKByLPnZ2dnUVZ5gOdnZ2d@giganews.com>
MIME-Version 1.0
Lines 70
NNTP-Posting-Date 29 Dec 2013 17:45:09 CET
NNTP-Posting-Host 2be31b47.newsspool1.arcor-online.net
X-Trace DXC=LHXXYVd2:ZK=8m7nZkdN^@ic==]BZ:afN4Fo<]lROoRAnkgeX?EC@@@0E8X5N4L0EBDZm8W4\YJNL[V>E<O5]2DKKiN==Vo67`FL5ZPYTaJ3NH
X-Complaints-To usenet-abuse@arcor.de
Xref csiph.com comp.lang.javascript:22475

Show key headers only | View raw


Robert Crandal wrote:

> In case anyone wants to see the actual code, here it is:
> 
> //
> // This code is supposed to initialize a slideshow over a
> // particular image gallery.
> //
> function startShow(obj) {
> 
>   var elem = tag("li", obj);    // Locate elements by tag
>   var gallery = id("gallery");  // Locate tag by ID
> 
>   for (var i = 0; i < elem.length; i++) new function() {
>     var cur = elem[i];
>     setTimeout(function() {
>         showImage(cur);
>         setTimeout(function() {
>            fadeOut(gallery, 0, 10);
>          }, 3500);
>     }, i * 5000);
>   }
>   setTimeout(hideOverlay, 5000 * elem.length);
>   showOverlay();
> }
> 
> I was just confused about the use of the "new function() {}" as
> seen above.  Hopefully someone can explain that to me.

It is the result of the common misconception that it would be necessary to 
call a function as a *constructor* in order to create a private scope.  
Evidently it does not make sense to call this function as a constructor; it 
does not even use “this”.

It is better written as follows:

  function startShow (obj)
  { 
    /* Locate elements by tag _name_ */
    var elem = tag("li", obj);

    /* Locate _element_ by ID */
    var gallery = id("gallery");
 
    for (var i = 0, len = elem.length; i < len; ++i)
    {
      (function (i) {
        var cur = elem[i];
        window.setTimeout(function () {
          showImage(cur);
          window.setTimeout(function () {
            fadeOut(gallery, 0, 10);
          }, 3500);
        }, i * 5000);
      }(i));
    }

    window.setTimeout(hideOverlay, 5000 * elem.length);
    showOverlay();
  }

Although these book reviews can be educational, it would take a lifetime 
explaining and correcting all of Resig's misconceptions.  I have better 
things to do, and so should you.

-- 
PointedEars

Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Wierd use of "new function() {}" "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-28 20:06 -0700
  Re: Wierd use of "new function() {}" "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-28 20:42 -0700
    Re: Wierd use of "new function() {}" Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-29 17:45 +0100
  Re: Wierd use of "new function() {}" Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-29 02:26 -0200
    Re: Wierd use of "new function() {}" John Harris <niam@jghnorth.org.uk.invalid> - 2013-12-29 11:07 +0000
      Re: Wierd use of "new function() {}" Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-29 11:52 -0200
      Re: Wierd use of "new function() {}" Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-12-29 19:39 +0000
        Re: Wierd use of "new function() {}" John Harris <niam@jghnorth.org.uk.invalid> - 2013-12-30 15:27 +0000

csiph-web