Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #22475
| Message-ID | <2053658.P1axspRLhA@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-12-29 17:45 +0100 |
| Subject | Re: Wierd use of "new function() {}" |
| Newsgroups | comp.lang.javascript |
| References | <ROqdnRynNfBWDCLPnZ2dnUVZ5u2dnZ2d@giganews.com> <Ks-dnQAIxZaKByLPnZ2dnUVZ5gOdnZ2d@giganews.com> |
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
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