i've been seeing syntax on few libraries , i'm wondering benefit is. (note i'm aware of closures , code doing, i'm concerned syntactical differences)
!function(){ // stuff }(); as alternative more common
(function(){ // stuff })(); for self invoking anonymous functions.
i'm wondering few things. first off, allowing top example work? why bang necessary in order make statement syntactically correct? i'm told + works, , i'm sure others, in place of !
second, benefit? can tell saves single character, can't imagine that's such huge benefit attract numerous adopters. there other benefit i"m missing?
the other difference can see return value of self invoking function, in both of these examples, don't care return value of function since it's used create closure. can tell me why 1 might use first syntax?
ideally should able as:
function(){ // stuff }(); that means declare anonymous function , execute it. not work due specifics of js grammar.
so shortest form of achieving use expression e.g. unaryexpression (and callexpression):
!function(){ // stuff }(); or fun:
-function(){ // stuff }(); or:
+function(){ // stuff }(); or even:
~function(){ // stuff return 0; }( );
Comments
Post a Comment