Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #22467 > unrolled thread
| Started by | "Robert Crandal" <noreply2me@yahoo.com> |
|---|---|
| First post | 2013-12-28 20:06 -0700 |
| Last post | 2013-12-30 15:27 +0000 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.javascript
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
| From | "Robert Crandal" <noreply2me@yahoo.com> |
|---|---|
| Date | 2013-12-28 20:06 -0700 |
| Subject | Wierd use of "new function() {}" |
| Message-ID | <ROqdnRynNfBWDCLPnZ2dnUVZ5u2dnZ2d@giganews.com> |
Another question about John Resig's book, "Pro
JavaScript Techniques" (C) 2006.
On page 210, the following JavaScript code is
seen:
for (i = 0; i < elem.length; i++) new function() {
/* code snipped here */
}
I don't think I've ever seen a "new function()" statement
used right after a "for" statement. Is this valid code that
is used for a certain purpose? Or am I looking at
another possible typo in the book?
Robert
[toc] | [next] | [standalone]
| From | "Robert Crandal" <noreply2me@yahoo.com> |
|---|---|
| Date | 2013-12-28 20:42 -0700 |
| Message-ID | <Ks-dnQAIxZaKByLPnZ2dnUVZ5gOdnZ2d@giganews.com> |
| In reply to | #22467 |
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.
Thanks!
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2013-12-29 17:45 +0100 |
| Message-ID | <2053658.P1axspRLhA@PointedEars.de> |
| In reply to | #22468 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2013-12-29 02:26 -0200 |
| Message-ID | <l9o88p$rqd$1@speranza.aioe.org> |
| In reply to | #22467 |
On 29/12/2013 01:06, Robert Crandal wrote:
> Another question about John Resig's book, "Pro
> JavaScript Techniques" (C) 2006.
>
> On page 210, the following JavaScript code is
> seen:
>
> for (i = 0; i < elem.length; i++) new function() {
>
> /* code snipped here */
> }
>
> I don't think I've ever seen a "new function()" statement
> used right after a "for" statement. Is this valid code that
> is used for a certain purpose? Or am I looking at
> another possible typo in the book?
>
new function () { } is a "constructor anonymous function", that creates
a new Object {}.
Douglas Crockford once wrote: "It is never a good idea to put new
directly in front of function. For example, new function provides no
advantage in constructing new objects."
<http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/>
Coming from a John Resig's book, we can expect everything (the good, the
bad and ugly).
--
Joao Rodrigues
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2013-12-29 11:07 +0000 |
| Message-ID | <5j00c91lgo66p9s4qbj8f24l56suvlh9dr@4ax.com> |
| In reply to | #22469 |
On Sun, 29 Dec 2013 02:26:02 -0200, Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote: <snip> >Douglas Crockford once wrote: "It is never a good idea to put new >directly in front of function. For example, new function provides no >advantage in constructing new objects." <snip> There's another dodgy author. What you've quoted may mislead people : "new objects" should be "new function objects". There are advantages when constructing other kinds of object. John
[toc] | [prev] | [next] | [standalone]
| From | Joao Rodrigues <groups_jr-1@yahoo.com.br> |
|---|---|
| Date | 2013-12-29 11:52 -0200 |
| Message-ID | <l9p9e3$tjo$1@speranza.aioe.org> |
| In reply to | #22471 |
On 29/12/2013 09:07, John Harris wrote: > On Sun, 29 Dec 2013 02:26:02 -0200, Joao Rodrigues > wrote: > > > <snip> >> Douglas Crockford once wrote: "It is never a good idea to put new >> directly in front of function. For example, new function provides no >> advantage in constructing new objects." > <snip> > > There's another dodgy author. If you compare JavaScript books' authors, I think that Douglas Crockford is not so bad. > What you've quoted may mislead people : > "new objects" should be "new function objects". There are advantages > when constructing other kinds of object. > Yes, what I've quoted may mislead people if they don't read the article containing Crockford's explanation: <http://yuiblog.com/blog/2006/11/13/javascript-we-hardly-new-ya/> But don't think that I'm one of his fanboys (if there are any). I don't like when Douglas rants against the equal operator (==) as if coercion was something to be avoided. Coercion is everywhere in JavaScript. -- Joao Rodrigues
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2013-12-29 19:39 +0000 |
| Message-ID | <0.709a04672be71524c96f.20131229193911GMT.87fvpb5twg.fsf@bsb.me.uk> |
| In reply to | #22471 |
John Harris <niam@jghnorth.org.uk.invalid> writes: > On Sun, 29 Dec 2013 02:26:02 -0200, Joao Rodrigues > <groups_jr-1@yahoo.com.br> wrote: > > <snip> >>Douglas Crockford once wrote: "It is never a good idea to put new >>directly in front of function. For example, new function provides no >>advantage in constructing new objects." > <snip> > > There's another dodgy author. What you've quoted may mislead people : > "new objects" should be "new function objects". There are advantages > when constructing other kinds of object. That would not fit. He is specifically talking about using "new function" to create objects that are *not* functions. -- Ben.
[toc] | [prev] | [next] | [standalone]
| From | John Harris <niam@jghnorth.org.uk.invalid> |
|---|---|
| Date | 2013-12-30 15:27 +0000 |
| Message-ID | <h643c9dsu39funtfh1r0ie5p8vc3c5q23m@4ax.com> |
| In reply to | #22481 |
On Sun, 29 Dec 2013 19:39:11 +0000, Ben Bacarisse <ben.usenet@bsb.me.uk> wrote: >John Harris <niam@jghnorth.org.uk.invalid> writes: > >> On Sun, 29 Dec 2013 02:26:02 -0200, Joao Rodrigues >> <groups_jr-1@yahoo.com.br> wrote: >> >> <snip> >>>Douglas Crockford once wrote: "It is never a good idea to put new >>>directly in front of function. For example, new function provides no >>>advantage in constructing new objects." >> <snip> >> >> There's another dodgy author. What you've quoted may mislead people : >> "new objects" should be "new function objects". There are advantages >> when constructing other kinds of object. > >That would not fit. He is specifically talking about using "new >function" to create objects that are *not* functions. Ah yes. I misread "new function" as "new Function". However, he still fails to describe the advantage of using "new". John
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web