Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function constructor vs function statement

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

like image 710
c4il Avatar asked Sep 26 '10 18:09

c4il


People also ask

What is difference between function and constructor function?

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.

What is the difference between function expression and function statement?

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.

What is a function constructor?

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() .

What is difference between constructor function and normal function in JavaScript?

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.

Is a constructor like a function?

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.

Can functions be called in constructor?

You can call a virtual function in a constructor. The Objects are constructed from the base up, “base before derived”.


2 Answers

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
like image 80
Cristian Sanchez Avatar answered Sep 28 '22 13:09

Cristian Sanchez


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).

like image 35
Marc Gravell Avatar answered Sep 28 '22 12:09

Marc Gravell