As you know in Javascript, it's very common to use the following expression as an IIFE (Immediately Invoked Function Expression) :
(function(){
//code ...
})();
I'm wondering if we can say that the following expression is an IIFE (when no return value needed):
new function(){
//code ...
}
Or
new function(global){
//code..
}(this);
Even though it's an object . Thanks .
Answer :
[ Thanks to @le_m , @vol7ron and @Bergi , here is the short answer ] Can we say that he following expression is an IIFE (Immediately Invoked Function Expression) ?
new function(){
//code ...
}
Or
new function(global){
//code..
}(this);
The answer is NO. And what's that ? It's just an unnamed object with an anonymous constructor, so we're not talking about functions here (plain and simple).
Is new function(){ ... }
an IIFE?
Now, IIFE stands for Immediately Invoked Function Expression. And a FunctionExpression is defined as follows:
function BindingIdentifierₒₚₜ ( FormalParameters ) { FunctionBody }
So, are we dealing with a function expression? Let's analyze your syntax:
"type": "ExpressionStatement",
"expression": {
"type": "NewExpression",
"callee": {
"type": "FunctionExpression",
"params": [],
"body": {
...
},
"arguments": []
}
This shows us that your code is not a function expression, but it includes a function expression as a part of the NewExpression.
Is this expression immediately invoked? In a way, yes - see [[Construct]] invocation. The constructor function is immediately invoked.
Still, I wouldn't call the whole thing an IIFE as the outer expression is not a function expression. Some call this expression Immeditately Invoked Constructor or IIC instead (thanks to @vol7ron, @Bergi for pointing this out).