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


Groups > comp.lang.javascript > #22246

Re: Anonymous self-invoking function

From Eric Bednarz <bednarz@fahr-zur-hoelle.org>
Newsgroups comp.lang.javascript
Subject Re: Anonymous self-invoking function
Organization Eric Conspiracy Secret Labs
References <RP-dneojCe_X4T_PnZ2dnUVZ5sWdnZ2d@giganews.com>
Date 2013-12-07 03:52 +0100
Message-ID <m28uvxcqyd.fsf@nntp.bednarz.nl> (permalink)

Show all headers | View raw


"Robert Crandal" <noreply2me@yahoo.com> writes:

> I'm studying some code in one of John Resig's books.

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).

> Here is a shortened/modified sample:

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!");
>   });
> }
>
> So, it's my understanding that the properties "this.assert"
> and "this.test" defined above are publicly accessible functions.

They are properties of the global object (unless that production is
contained in ES5 strict mode code; the publication might predate ES5,
or not, who knows). Saying "they are [...] functions" is like saying
"food vouchers are nutritious".

> (Since the "var" keyword is missing this makes them
> properties of the

They are *explicitly* set as object properties (assuming `this` is in
fact not undefined).

> global window object, right?).

You want to remove `window` from that.

> I don't fully understand why the self-invoking anonymous function
> is needed above.  Is there a benefit to defining the functions above,
> such as, "this.assert = function(a,b) {};" inside of the anonymous
> function??

The most common use case would be creating an execution context for
'private' variables and/or functions that are visible to the functions
that are referenced by the global properties.

Technically, you can delete those properties (unlike variable and
function declarations).

For more far fetched senarios, that might be some module that can be
used as is for simple use cases and be integrated into some bigger API
by having a build script replacing '()' with '.call(someApiObject)'.

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


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