Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #22252
| Message-ID | <1963861.2MBlU7BgDz@PointedEars.de> (permalink) |
|---|---|
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Organization | PointedEars Software (PES) |
| Date | 2013-12-08 00:17 +0100 |
| Subject | Re: Anonymous self-invoking function |
| Newsgroups | comp.lang.javascript |
| References | <RP-dneojCe_X4T_PnZ2dnUVZ5sWdnZ2d@giganews.com> <m28uvxcqyd.fsf@nntp.bednarz.nl> <T_ydnZ1AqOvn6j7PnZ2dnUVZ5oOdnZ2d@giganews.com> |
Robert Crandal wrote:
> "Eric Bednarz" wrote:
>> Although the combination of author and group you posted to is unlikely
>> to yield productive answers, you should at least provide the book title
>> and the chapter (ebooks make page numbers problematic).
>
> It was "Secrets of the JavaScript Ninja", (c) 2013, by John Resig
> and Bear Bibeault. The code example is in Chapter 2, section 2.4.2,
> page 24.
IIRC that or the previous book by Resig was a one that we discussed here
already, and that started with stating utter nonsense like "HTML is an
application of XML" or something similar stupid. Rest assured that John
Resig still does not have a clue about what he is doing – as evidenced by
the inefficient bloated mess that still is jQuery – but he hides it well,
and many people – especially the uninitiated – fall for appearances (as a
poor substitute for logic). Any “secrets” he might be able to reveal to you
in there would have been discovered by other people, some posting in this
newsgroup, years before he even started, or are not secrets in any meaning
of the word at all.
In particular, the so-called Module Pattern (an “anonymous self-invoking
function”) was popularized, if not invented, by Douglas Crockford, who used
to post here (before he went down the path of madness that so far and AFAIK
all JS book authors went). However, AFAIK even Crockford was not so crazy
as to suggest you should use “this” in there like that.
>> In any case you should explain exactly what you shortened and/or
>> modified.
>>
>>> /* Anonymous self-invoking function */
>>> (function() {
>>> this.assert = function assert(a,b) { /* code here */ };
>>> this.test = function test(a, b) { /* code here */ };
>>> })();
>>>
>>> /* Code for a testing environment or "test groups" */
>>> window.onload = function() {
>>> test("Test one.", function() {
>>> assert(true, "Success!");
>>> });
>>> }
>>>
>
> The only code that I omitted was the body of the assert()
> and test() functions, and little bit of the code inside the
> window.onload section, which is basically just:
>
> window.onload = function() {
> /* code here that calls test() and assert() */
> };
>
> Basically, assert() and test() and defined and made publicly
> accessible in the anonymous self-invoking function as seen
> above. These functions, test() and assert(), are then called
> during window.onload. That's the basic gist.
This code, by your own account from a book published/edited in 2013,
*breaks* in strict mode (with a TypeError in the first line of the function
body, because “this” is the “undefined” value then), which was introduced
with ECMAScript Edition 5 in December *2010*, and is recommended for
production-quality code since then. QED.
That the functions whose references are assigned to the properties of the
ECMAScript global object (if not in strict mode) are named is particularly
unnecessary and error-prone because of the known JScript bug. Why would an
“assert” function need a name, why would it need to call itself?
Compatibility cannot be the reason for the naming because the outer function
expression already is anonymous.
The proper way is either to explicitly call the function with a reference to
the global object as the “this” value, or read “this” in global context into
a variable and use that variable (and to avoid window.onload):
(function (global) {
global.assert = function () { … };
}).call(this);
or
(function (global) {
global.assert = function () { … };
}).call(null);
or
(function (global) {
global.assert = function () { … };
}(this));
Since the latter suffices, and the former two are less compatible, the
latter should be used. On the other hand, if the private scope is not
needed, the proper way is
function assert ()
{
// …
}
function test ()
{
// …
}
As for those two functions, however, it is better to have them *not* in the
global namespace by default so that they do not interfere with the (test
case) code that is using them. See also
<http://PointedEars.de/wsvn/JSX/trunk/test/test.js>.
--
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
Anonymous self-invoking function "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-06 18:39 -0700
Re: Anonymous self-invoking function Eric Bednarz <bednarz@fahr-zur-hoelle.org> - 2013-12-07 03:52 +0100
Re: Anonymous self-invoking function "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-07 12:31 -0700
Re: Anonymous self-invoking function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-08 00:17 +0100
Re: Anonymous self-invoking function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-08 01:08 +0100
Re: Anonymous self-invoking function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-08 19:41 +0100
Re: Anonymous self-invoking function jaranmclarn@gmail.com - 2013-12-08 12:21 -0800
Re: Anonymous self-invoking function "Robert Crandal" <noreply2me@yahoo.com> - 2013-12-09 01:14 -0700
Re: Anonymous self-invoking function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-10 15:38 +0100
Re: Anonymous self-invoking function Bjoern Hoehrmann <bjoern@hoehrmann.de> - 2013-12-07 12:28 +0100
Re: Anonymous self-invoking function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-07 12:57 +0100
Re: Anonymous self-invoking function Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2013-12-07 16:51 -0200
Re: Anonymous self-invoking function jaranmclarn@gmail.com - 2013-12-08 12:17 -0800
Re: Anonymous self-invoking function Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-12-09 00:15 +0100
csiph-web