f



How to re-write the following?

Hi,

Current, I have a function, that is working perfectly.
It's like this:	
	myFN = function() { ... }

I wonder if we can rewrite it into something like below
( function() {}
());

Then, how would myFN fit into it?

Thanks.
0
justaguy
10/21/2016 4:06:06 AM
comp.lang.javascript 38370 articles. 2 followers. javascript4 (1315) is leader. Post Follow

4 Replies
363 Views

Similar Articles

[PageSpeed] 12

On Thu, 20 Oct 2016 21:06:06 -0700 (PDT), justaguy wrote:
> Hi,
> 
> Current, I have a function, that is working perfectly.
> It's like this:	
> 	myFN = function() { ... }
> 
> I wonder if we can rewrite it into something like below
> ( function() {}
> ());
> 
> Then, how would myFN fit into it?
> 
> Thanks.

You can't if you need to refer the function more than once.
Otherwise, just copy the function body. e.g.:

  myFN = function() {
    console.log(123);
    console.log(456);
  };

Become:

  (function() {
    console.log(123);
    console.log(456);
  })();
0
JJ
10/21/2016 1:46:25 PM
On Friday, October 21, 2016 at 9:46:29 AM UTC-4, JJ wrote:
> On Thu, 20 Oct 2016 21:06:06 -0700 (PDT), justaguy wrote:
> > Hi,
> > 
> > Current, I have a function, that is working perfectly.
> > It's like this:	
> > 	myFN = function() { ... }
> > 
> > I wonder if we can rewrite it into something like below
> > ( function() {}
> > ());
> > 
> > Then, how would myFN fit into it?
> > 
> > Thanks.
> 
> You can't if you need to refer the function more than once.
> Otherwise, just copy the function body. e.g.:
> 
>   myFN = function() {
>     console.log(123);
>     console.log(456);
>   };
> 
> Become:
> 
>   (function() {
>     console.log(123);
>     console.log(456);
>   })();

ok, thanks.
0
justaguy
10/21/2016 3:42:30 PM
JJ wrote:

> On Thu, 20 Oct 2016 21:06:06 -0700 (PDT), justaguy wrote:
>> Current, I have a function, that is working perfectly.
>> It's like this:
>> myFN = function() { ... }
>> 
>> I wonder if we can rewrite it into something like below
>> ( function() {}
>> ());
>> 
>> Then, how would myFN fit into it?
> 
> You can't if you need to refer the function more than once.

The question is sufficiently unclear¹ to allow for several interpretations 
of it (and the OP is not available on my server).  Perhaps what was really 
being asked is how to create a private namespace in which the function is 
defined, which is a common task.  If so, then one way would be to change

  var myFN = function () {
    // …
  };

into

  var myFN = (function () {
    "use strict";

    /* private variables */

    return function () {
      // …
    };
  }());

while the function call remains the same.  Or into the module pattern –

  var my = (function () {
    "use strict";

    /* private variables */

    return {
      fn: function () {
        // …
      }
    };
  }());

– and then instead of

  myFN();

write

  my.fn();

Note that “this” in the function will then refer to the same object as “my”.

Also note how a function is defined and called differ in my code style so 
that it becomes easier to tell them apart.

________
¹  see <http://catb.org/esr/faqs/smart-questions.html>
-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.
0
Thomas
10/21/2016 4:51:30 PM
On Friday, October 21, 2016 at 12:06:26 AM UTC-4, justaguy wrote:
> Hi,
> 
> Current, I have a function, that is working perfectly.
> It's like this:	
> 	myFN = function() { ... }
> 
> I wonder if we can rewrite it into something like below
> ( function() {}
> ());
> 
> Then, how would myFN fit into it?
> 
> Thanks.

Helpful.  Thank you.
0
justaguy
10/21/2016 10:11:56 PM
Reply: