Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #31592 > unrolled thread
| Started by | justaguy <lichunshen84@gmail.com> |
|---|---|
| First post | 2016-10-20 21:06 -0700 |
| Last post | 2016-10-21 15:11 -0700 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.javascript
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
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2016-10-20 21:06 -0700 |
| Subject | How to re-write the following? |
| Message-ID | <954e5297-c7f0-4aee-a838-a2c44e356b88@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | JJ <jj4public@vfemail.net> |
|---|---|
| Date | 2016-10-21 20:46 +0700 |
| Message-ID | <fr9r2d5see84.fy6vbbjg5xxt$.dlg@40tude.net> |
| In reply to | #31592 |
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);
})();
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2016-10-21 08:42 -0700 |
| Message-ID | <18d2c65f-720a-4c53-a034-57468b8b902f@googlegroups.com> |
| In reply to | #31594 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2016-10-21 17:51 +0100 |
| Message-ID | <4239102.31r3eYUQgx@PointedEars.de> |
| In reply to | #31594 |
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.
[toc] | [prev] | [next] | [standalone]
| From | justaguy <lichunshen84@gmail.com> |
|---|---|
| Date | 2016-10-21 15:11 -0700 |
| Message-ID | <ca93e55b-f1ea-40f3-ad1a-8877ca4dbf4c@googlegroups.com> |
| In reply to | #31592 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web