[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

And Another function Question

Gene Wirchenko

12/9/2015 6:56:00 PM

Hello:

What does (contrived example follows)
(function(a,b,c){some_code})(1,2,3);
mean?

Is this to define an unnamed function with code some_code and
then immediately call it with the parms 1,2,3?

What is a good reference for this? I am willing to put in study
time, but I do not even know where to start.

Sincerely,

Gene Wirchenko
9 Answers

Luuk

12/9/2015 8:14:00 PM

0

On 09-12-15 19:55, Gene Wirchenko wrote:
> Hello:
>
> What does (contrived example follows)
> (function(a,b,c){some_code})(1,2,3);
> mean?
>
> Is this to define an unnamed function with code some_code and
> then immediately call it with the parms 1,2,3?
>
> What is a good reference for this? I am willing to put in study
> time, but I do not even know where to start.
>
> Sincerely,
>
> Gene Wirchenko
>


you could start here:
http://www.ecmas...

or here:
http://www.ecma-international.org/publications/files/ECMA-ST/EC...


JR

12/9/2015 8:50:00 PM

0

Em 09/12/2015 16:55, Gene Wirchenko escreveu:
> Hello:
>
> What does (contrived example follows)
> (function(a,b,c){some_code})(1,2,3);
> mean?
>
> Is this to define an unnamed function
^^^^^^^^
I'd rather say an "anonymous" function.

> with code some_code and
> then immediately call it with the parms 1,2,3?

Exactly.

>
> What is a good reference for this? I am willing to put in study
> time, but I do not even know where to start.
>

Ben Alman has written a good article about IIFEs:
<http://benalman.com/news/2010/11/immediately-invoked-function-expre...


--
Joao Rodrigues

Ben Bacarisse

12/9/2015 9:48:00 PM

0

Gene Wirchenko <genew@telus.net> writes:

> What does (contrived example follows)
> (function(a,b,c){some_code})(1,2,3);
> mean?
>
> Is this to define an unnamed function with code some_code and
> then immediately call it with the parms 1,2,3?

Yes.

> What is a good reference for this? I am willing to put in study
> time, but I do not even know where to start.

Sorry, I can't help there. The general notion -- that of anonymous
functions -- is found in many languages. If you knew, for example,
Lisp, almost nothing in EcmaScript would be new to you, but you probably
want a language specific reference, and I have not researched that.

--
Ben.

ram

12/9/2015 10:21:00 PM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>Sorry, I can't help there. The general notion -- that of anonymous
>functions -- is found in many languages. If you knew, for example,
>Lisp, almost nothing in EcmaScript would be new to you, but you probably
>want a language specific reference, and I have not researched that.

»14.1 Function Definitions« gives the syntax for
»FunctionExpression« and says that a »FunctionExpression«
starts with the token »function«.

»13.5 Expression Statement« says that an
»ExpressionStatement« cannot start with the token
»function«.

function() { console.log("ok."); } ();
SyntaxError

( function() { console.log("ok."); } () );
"ok."

Thomas 'PointedEars' Lahn

12/9/2015 11:26:00 PM

0

Gene Wirchenko wrote:

> What does (contrived example follows)
> (function(a,b,c){some_code})(1,2,3);
> mean?
>
> Is this to define an unnamed function with code some_code and
> then immediately call it with the parms 1,2,3?

Yes. The proper term is â??anonymous function (expression)â? rather than
â??unnamed functionâ?.

> What is a good reference for this? I am willing to put in study
> time, but I do not even know where to start.

<https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Function_expre...

Also referred to by â?? you probably have guessed it â?? the FAQ.

--
PointedEars
FAQ: <http://PointedEars.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Ben Bacarisse

12/10/2015 1:10:00 AM

0

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>Sorry, I can't help there. The general notion -- that of anonymous
>>functions -- is found in many languages. If you knew, for example,
>>Lisp, almost nothing in EcmaScript would be new to you, but you probably
>>want a language specific reference, and I have not researched that.
>
> »14.1 Function Definitions« gives the syntax for
> »FunctionExpression« and says that a »FunctionExpression«
> starts with the token »function«.

Sure, but I did not think the OP wanted a reference to the language
specification. I though they wanted some resource to learn about such
usages. By "language specific reference" I meant something to learn
about this style but using EcmaScript not, say, Lisp or ML.

--
Ben.

ram

12/10/2015 1:49:00 AM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>ram@zedat.fu-berlin.de (Stefan Ram) writes:
>>Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>>>Sorry, I can't help there. The general notion -- that of anonymous
>>>functions -- is found in many languages. If you knew, for example,
>>>Lisp, almost nothing in EcmaScript would be new to you, but you probably
>>>want a language specific reference, and I have not researched that.
>>»14.1 Function Definitions« gives the syntax for
>>»FunctionExpression« and says that a »FunctionExpression«
>>starts with the token »function«.
>Sure, but I did not think the OP wanted a reference to the language
>specification. I though they wanted some resource to learn about such
>usages. By "language specific reference" I meant something to learn
>about this style but using EcmaScript not, say, Lisp or ML.

Ah, I see!

I believe many JavaScript books and tutorials for beginners treat
function expression, albeit sometimes under a different name.

One can skim through the table of contents, the index,
or the pages of a book to get an impression before buying.

A modern book also would treat modern function expressions, like

x => x * x

, which are actually called »arrow functions« (14.2).

( x => x * x )( 2 )
4

I had to add parentheses there too, albeit for a different
reason.

RobG

12/10/2015 3:24:00 AM

0

On Thursday, December 10, 2015 at 6:13:19 AM UTC+10, Luuk wrote:
[...]
> you could start here:
> http://www.ecmas...
>
> or here:
> http://www.ecma-international.org/publications/files/ECMA-ST/EC...

Expecting a novice to understand immediately invoked function expressions simply by reading the latest language specification is not realistic. If it was helpful, it would be the answer to most of the ECMAScript questions asked here.

But it's not, so it isn't.

--
Rob

Luuk

12/10/2015 7:07:00 PM

0

On 10-12-15 04:23, RobG wrote:
> On Thursday, December 10, 2015 at 6:13:19 AM UTC+10, Luuk wrote:
> [...]
>> you could start here:
>> http://www.ecmas...
>>
>> or here:
>> http://www.ecma-international.org/publications/files/ECMA-ST/EC...
>
> Expecting a novice to understand immediately invoked function expressions simply by reading the latest language specification is not realistic. If it was helpful, it would be the answer to most of the ECMAScript questions asked here.
>

The OP was not claiming to be a novice...

> But it's not, so it isn't.
>

So, he's not (a novice).....


;)