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


Groups > comp.lang.javascript > #22250

Re: Anonymous self-invoking function

From Joao Rodrigues <groups_jr-1@yahoo.com.br>
Newsgroups comp.lang.javascript
Subject Re: Anonymous self-invoking function
Date 2013-12-07 16:51 -0200
Organization Aioe.org NNTP Server
Message-ID <l7vqn4$6r6$1@speranza.aioe.org> (permalink)
References <RP-dneojCe_X4T_PnZ2dnUVZ5sWdnZ2d@giganews.com>

Show all headers | View raw


On 06/12/2013 23:39, Robert Crandal wrote:
> I'm studying some code in one of John Resig's books.
> Here is a shortened/modified sample:
>
> /* 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.
> (Since the "var" keyword is missing this makes them
> properties of the global window object, right?).

Technically speaking, they are methods of the global object ("window" in 
web browsers), and this has nothing to do with the missing var keyword. 
And we could do the same as in

(function (global) {
   global.assert = function (a,b) { /* code here */  };
   global.test = function (a, b) { /* code here */ };
}(this));

But that is a bad idea since web browsers could implement such functions 
as native ones in future versions, or we could have conflicts with other 
libraries implementing such functions.

Also there is no need to name function expressions, unless the author 
intends to use recursion or use the debugging tools of modern browsers:

   this.assert = function assert(a,b) { /* code here */  };

We could simply use the anonymous function expression form:

   this.assert = function (a,b) { /* code here */  };

-- 
Joao Rodrigues

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