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


Groups > comp.lang.javascript > #14252

Re: function(){}() and subsequent calls

Date 2012-05-31 17:12 +1000
From Andrew Poulos <ap_prog@hotmail.com>
Newsgroups comp.lang.javascript
Subject Re: function(){}() and subsequent calls
References <z42dnVxvhJ19m1rSnZ2dnUVZ_vCdnZ2d@westnet.com.au> <jq74ed$s3p$1@news01.versatel.de>
Message-ID <fcidnaenHatRhFrSnZ2dnUVZ_qCdnZ2d@westnet.com.au> (permalink)

Show all headers | View raw


On 31/05/2012 4:51 PM, Holger Suhr wrote:
> Am 31.05.2012 07:51, schrieb Andrew Poulos:
>> With
>> var foo = function() {}()
>> if I later call
>> foo()
>> I get an error. Whereas using
>> var foo = function() {}
>> lets me call
>> foo()
>>
>> Adding the succeeding parentheses calls the function but does it alter
>> it as well?
>>
>> Andrew Poulos
>
> var foo = function(){}();
>
> executes the no-name function and assingns the return value to foo.
> typeof foo == "undefined"
>
> var foo = function(){return true;}();
> typeof foo == "boolean"
>
> var foo = function(){};
> assigns the function to foo
> typeof foo == "function"
>
> var foo = function(){return true;};
> typeof foo == "function"

Ah, so if I want it called immediately and still be available for 
subsequent calls I tried this
   var foo;
   (foo = function(){})();
which "works" but JSLint says "Bad invocation".

BTW I notice that <http://www.jslint.com/> has a nice new UI.

Andrew Poulos

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


Thread

function(){}() and subsequent calls Andrew Poulos <ap_prog@hotmail.com> - 2012-05-31 15:51 +1000
  Re: function(){}() and subsequent calls Holger Suhr <holgerholgerde@yahoo.de> - 2012-05-31 08:51 +0200
    Re: function(){}() and subsequent calls Andrew Poulos <ap_prog@hotmail.com> - 2012-05-31 17:12 +1000
      Re: function(){}() and subsequent calls Gregor Kofler <usenet@gregorkofler.com> - 2012-05-31 13:40 +0200
      Re: function(){}() and subsequent calls Matt McDonald <matt@fortybelow.ca> - 2012-05-31 09:57 -0600
        Re: function(){}() and subsequent calls Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-06-01 01:12 +0200

csiph-web