Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

! function in javascript?

I apologize for my stupid question, I am not good at javascript and angularjs. I’ve been referencing the code of some projects, and I’m really confused about this, what it actually is !function(a,b,c){}(x,y,z). I have never seen it before. I am not good at javascript nor angularjs. Thanks for all the help!

!function (e, n, t) {
    "use strict";
    // code
}(window, angular, jQuery)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It’s a hacky way to immediately execute an anonymous function. If you were to define it normally (line starting with function, then it would immediately hoist the function as a function statement and force you to call it normally. Since the ! is in front of it, that lets the compiler evaluate it as a function expression, which can be invoked immediately.

This would be equivalent code:

(e,n,t) => { /** */ }(window, angular, jQuery);

as would

(function (e,n,t) { /** */ })(window, angular, jQuery);

since the parenthesis will make the line be evaluated as an expression as well.

The anonymous function is being defined, and then called with the parameters window, angular, jQuery, which become e,n,t inside the function body.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading