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


Groups > comp.lang.javascript > #31596

Re: How to re-write the following?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.javascript
Subject Re: How to re-write the following?
Date 2016-10-21 17:51 +0100
Organization PointedEars Software (PES)
Message-ID <4239102.31r3eYUQgx@PointedEars.de> (permalink)
References <954e5297-c7f0-4aee-a838-a2c44e356b88@googlegroups.com> <fr9r2d5see84.fy6vbbjg5xxt$.dlg@40tude.net>

Show all headers | View raw


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.

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


Thread

How to re-write the following? justaguy <lichunshen84@gmail.com> - 2016-10-20 21:06 -0700
  Re: How to re-write the following? JJ <jj4public@vfemail.net> - 2016-10-21 20:46 +0700
    Re: How to re-write the following? justaguy <lichunshen84@gmail.com> - 2016-10-21 08:42 -0700
    Re: How to re-write the following? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2016-10-21 17:51 +0100
  Re: How to re-write the following? justaguy <lichunshen84@gmail.com> - 2016-10-21 15:11 -0700

csiph-web