[lnkForumImage]
TotalShareware - Download Free Software

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


 

Andrew Poulos

5/7/2015 10:15:00 AM

I was looking at some "swipe" libraries when I happened upon hammer.js.
<http://hammerjs.github.io/dist/hammer....

I've no idea yet as to its quality but the very first line begins with

! function(a, b, c, d) {

What's the reason behind starting with an exclamation mark?

Andrew Poulos
18 Answers

Evertjan.

5/7/2015 11:04:00 AM

0

Andrew Poulos <ap_prog@hotmail.com> wrote on 07 mei 2015 in
comp.lang.javascript:

> I was looking at some "swipe" libraries when I happened upon hammer.js.
> <http://hammerjs.github.io/dist/hammer....
>
> I've no idea yet as to its quality but the very first line begins with
>
>! function(a, b, c, d) {
>
> What's the reason behind starting with an exclamation mark?

!function(a,b,c,d){"use strict";[..

...]}(window,document,"Hammer");

Google is your friend!

The unary operator ! (as do ~, -, +) allows the parameters in the () at the
end to invoke the function in-place and hides the function name from the
global, and any other [only outside?] scope/namespace.

<http://stackoverflow.com/questions/3755606/what-does-the-exclama...
do-before-the-function>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Michael Haufe (\"TNO\")

5/7/2015 12:03:00 PM

0

On Thursday, May 7, 2015 at 5:15:23 AM UTC-5, Andrew Poulos wrote:
> I was looking at some "swipe" libraries when I happened upon hammer.js.
> <http://hammerjs.github.io/dist/hammer....
>
> I've no idea yet as to its quality but the very first line begins with
>
> ! function(a, b, c, d) {
>
> What's the reason behind starting with an exclamation mark?

It's a recent trend to replace (function(){...})(); you could use other operators as well

JR

5/7/2015 3:28:00 PM

0

On 07/05/2015 07:15, Andrew Poulos wrote:
> I was looking at some "swipe" libraries when I happened upon hammer.js.
> <http://hammerjs.github.io/dist/hammer....
>
> I've no idea yet as to its quality but the very first line begins with
>
> ! function(a, b, c, d) {
>
> What's the reason behind starting with an exclamation mark?


An Immediately-Invoked Function Expression (IIFE) is usually written as:
(function() { /* etc. */ })();

Or:

(function() { /* etc. */ }()); // Douglas Crockford's preference.

But we can use unary operators instead of the wrapping parentheses too,
if we don't care about the return value of the IIFE:

+function() { /*...*/ }();
!function() { /*...*/ }();
-function() { /*...*/ }();
~function() { /*...*/ }();

Even the void operator can be used:
void function () { /*...*/ }();

--
Joao Rodrigues

JJ

5/7/2015 5:09:00 PM

0

On Thu, 07 May 2015 12:27:55 -0300, Joao Rodrigues wrote:
>
> An Immediately-Invoked Function Expression (IIFE) is usually written as:
> (function() { /* etc. */ })();
>
> Or:
>
> (function() { /* etc. */ }()); // Douglas Crockford's preference.
>
> But we can use unary operators instead of the wrapping parentheses too,
> if we don't care about the return value of the IIFE:
>
> +function() { /*...*/ }();
> !function() { /*...*/ }();
> -function() { /*...*/ }();
> ~function() { /*...*/ }();
>
> Even the void operator can be used:
> void function () { /*...*/ }();

With unary operators, it will make the JavaScript engine evaluate the
function return value using that operator. In theory, this will add more
processing time although it's very tiny.

With (), e.g. (function(){})(), there's no additional processing time.

But what about when using void? e.g. void function(){}()
If I understand it correctly, the function return value won't be evaluated,
but it does add additional code which replace the final expression with
undefined.

Evertjan.

5/7/2015 7:07:00 PM

0

Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote on 07 mei 2015 in
comp.lang.javascript:

> On 07/05/2015 07:15, Andrew Poulos wrote:
>> I was looking at some "swipe" libraries when I happened upon hammer.js.
>> <http://hammerjs.github.io/dist/hammer....
>>
>> I've no idea yet as to its quality but the very first line begins with
>>
>> ! function(a, b, c, d) {
>>
>> What's the reason behind starting with an exclamation mark?
>
>
> An Immediately-Invoked Function Expression (IIFE) is usually written as:
> (function() { /* etc. */ })();
>
> Or:
>
> (function() { /* etc. */ }()); // Douglas Crockford's preference.
>
> But we can use unary operators instead of the wrapping parentheses too,
> if we don't care about the return value of the IIFE:
>
> +function() { /*...*/ }();
>!function() { /*...*/ }();
> -function() { /*...*/ }();
> ~function() { /*...*/ }();
>
> Even the void operator can be used:
> void function () { /*...*/ }();

Even nothing can be used,
the final parenteses with parameters work then too,
try:

alert( function(a){alert(a)} ) // function(a){alert(a)}
// [no execution of the function here]

alert( !function(a){alert(a)} ) // false
// [no execution of the function here]

alert( function(a){alert(a)}('executed') ) // executed // undefined

alert( !function(a){alert(a)}('executed') ) // executed // true

alert( void function(a){alert(a)}('executed') ) // executed // undefined





--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Christoph M. Becker

5/7/2015 7:32:00 PM

0

Evertjan. wrote:

> Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote on 07 mei 2015 in
> comp.lang.javascript:
>
>> An Immediately-Invoked Function Expression (IIFE) is usually written as:
>> (function() { /* etc. */ })();
>>
>> But we can use unary operators instead of the wrapping parentheses too,
>> if we don't care about the return value of the IIFE:
>>
>> Even the void operator can be used:
>> void function () { /*...*/ }();
>
> Even nothing can be used,
> the final parenteses with parameters work then too,
> try:
>
> alert( function(a){alert(a)} ) // function(a){alert(a)}
> // [no execution of the function here]

That works, because the IIFE is parsed in an expression context. If the
IIFE is placed where a statement is expected, that would be a syntax
error. The whole point of using parens or an operator is to enforce an
expression context.

--
Christoph M. Becker

Evertjan.

5/7/2015 8:21:00 PM

0

"Christoph M. Becker" <cmbecker69@arcor.de> wrote on 07 mei 2015 in
comp.lang.javascript:

> Evertjan. wrote:
>
>> Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote on 07 mei 2015 in
>> comp.lang.javascript:
>>
[..]
>>> Even the void operator can be used:
>>> void function () { /*...*/ }();
>>
>> Even nothing can be used,
>> the final parenteses with parameters work then too,
>> try:
>>
>> alert( function(a){alert(a)} ) // function(a){alert(a)}
>> // [no execution of the function here]
>
> That works, because the IIFE is parsed in an expression context. If the
> IIFE is placed where a statement is expected, that would be a syntax
> error.

Indeed, so you can use this [the first is sort of standard]:

var r = function(a){alert(a)} // no execution yet!
alert(r) // function(a){alert(a)}

var r = function(a){alert(a)}('execution') // execution
alert(r) // undefined

> The whole point of using parens or an operator is

Not true.

While that could be your point,
it was not the point of the OP, who was just Qing.

Methinks understanding how it works
is an important prerequisite for why it is used.

> to enforce an expression context.

No "expression context" [I hope you mean 'scope'] is forced,
the return value is just accepted as required.

(function(a){alert(a)}('hi')) // hi

This works like void.

>> An Immediately-Invoked Function Expression (IIFE) is usually written as:
>> (function() { /* etc. */ })();

THIS DOES NOT WORK!

============================================

But why does this work:

-function(a){alert(a);return 2}('hi')

and this does not:

function(a){alert(a);return -2}('hi')

??

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

JR

5/7/2015 8:47:00 PM

0

On 07/05/2015 16:31, Christoph M. Becker wrote:

> The whole point of using parens or an operator is to enforce an
> expression context.
>

Brilliant! That sums it up.

--
Joao Rodrigues

Evertjan.

5/7/2015 9:22:00 PM

0

Joao Rodrigues <groups_jr-1@yahoo.com.br> wrote on 07 mei 2015 in
comp.lang.javascript:

> On 07/05/2015 16:31, Christoph M. Becker wrote:
>
>> The whole point of using parens or an operator is to enforce an
>> expression context.
>>
>
> Brilliant! That sums it up.

Well no, it does not.

It does not cover the "how".

And it does not force a "scope", if that was ment
by the stange wording "expression context".

If there is a point,
it must be just to suppress the error of not allowing
a anonymous function to have a return value go into limbo.

The "how" is not even clear,
as the ! just changes the value to boolean,
and the + an - just make it numeric.

It seems orphan return values are allowed,
as long as they don't originate from a anonymous function:

'hi'; alert('x');
2+7; alert('y');

Anyone?

-
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)

Christoph M. Becker

5/7/2015 9:28:00 PM

0

Evertjan. wrote:

> "Christoph M. Becker" <cmbecker69@arcor.de> wrote on 07 mei 2015 in
> comp.lang.javascript:
>
>> to enforce an expression context.
>
> No "expression context" [I hope you mean 'scope'] is forced,
> the return value is just accepted as required.

No, I don't mean "scope". I was referring to *parsing*; however, the
term "expression context" might be misleading.

> But why does this work:
>
> -function(a){alert(a);return 2}('hi')
>
> and this does not:
>
> function(a){alert(a);return -2}('hi')
>
> ??

This is a syntax issue. See annex A of ECMA-262 5.1 Edition[1]. The
first line is parsed as (simplified)

SourceElement -> Statement -> Expression -> UnaryExpression -> -
UnaryExpression -> - FunctionExpression Arguments -> ...

while the second line is parsed as (again simplified)

SourceElement -> FunctionDeclaration -> ...

and the remaining ('hi') is a syntax error, as no ASI applies here.

[1] <http://www.ecma-international.org/ecma-262/5.1/...

--
Christoph M. Becker