today I've read we have a way of declaring the function by Function constructor. But I never seen the actual implementation that uses Function
constructor in real. So I would like to ask, are there any circumstances that we can get benefit by using Function
constructor as opposed to using function()
declaration? And what are the hidden differences of between?(if there is any)
Function Constructor
var func = new Function("x", "y", "return x*y;"); // pass the context by String
function():
var func = function(x, y){ return x*y; }
Thanks
Answer. Constructor is a block of code that initializes a newly created object. Function is a group of statements that can be called at any point in the program using its name to perform a specific task.
A Function Expression works just like a function declaration or a function statement, the only difference is that a function name is NOT started in a function expression, that is, anonymous functions are created in function expressions.
Function() constructor. The Function() constructor creates a new Function object. Calling the constructor directly can create functions dynamically, but suffers from security and similar (but far less significant) performance issues as eval() .
A constructor function is a normal function. What makes the difference here is the use of the new operator which makes the context ( this ) in the function the new instance, thus letting it take the two properties, and returns this new instance.
A constructor is a function that creates an instance of a class which is typically called an “object”. In JavaScript, a constructor gets called when you declare an object using the new keyword. The purpose of a constructor is to create an object and set values if there are any object properties present.
You can call a virtual function in a constructor. The Objects are constructed from the base up, “base before derived”.
The Function constructor is a form of eval
, which should generally be avoided (it's slow and considered insecure). There is really no benefit to using the Function constructor over the built in function statement unless you want to construct a function from dynamic components, which is pretty rare. There are legitimate uses for this form, however most of the time it's used unnecessarily which is why it's looked down upon and generally avoided.
Also, functions created with the function constructor will not keep a reference to the environment they were defined in (the closure). When executed, they will pull those variables directly from the global scope.
var f, a;
(function () {
var a = 123;
f = new Function("return a");
})();
f(); //undefined
a = "global"
f(); // "global"
Whereas regular functions do keep a reference to the environment in which they were defined:
var f;
(function () {
var a = 123;
f = function () { return a; }
})();
f(); //123
Well, the obvious difference when working with strings is that you have the option of meta-programming, by constructing the string at runtime (the same as you could with eval
). However, this is double edged, and arguably leads into a range of other problems with literal concatenation (injection), and perhaps simply complexity. If you don't need the string version, I wouldn't use it to be honest.
A side benefit of the regular (non-string) version is that most javascript minifiers (or obfuscators) will know what to do with it. This seems unlikely for the string, i.e. they'll leave it "as is" (not minified or obfuscated).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With