0

I have seen usage like var f1=new Function(); but no more than that. Tried to search in Google regarding Function-class but nothing returned what I needed.

What is its significance?

Please suggest me how and when to use it. If possible give me some genuine link to study about it.

1
  • This question is similar to: Legitimate uses of the Function constructor. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Sep 29 at 21:46

4 Answers 4

3

Best practice suggest you should never use a base type constructor functions. In a lot of case, use new with a base type will break the usage of typeof.

But, new Function() is valid JavaScript. Just not recommended, and not useful in most cases (var a= function() {} is equivalent).

Not that you can use the Function constructor as an eval call like so new Function('var a = 1; return a + a;'). But this can be dangerous, so use carefully.

Sign up to request clarification or add additional context in comments.

5 Comments

@JonathanLonowski Agreed. That's why I added the note on the eval capacity.
Thanx a lot. I actually needed this one.
can I define the function f1 after declaring it as: var f1=new Function(); i.e. I want to define it later than declaring it with Function constructor. Is there any way to do it?
In the same scope you can't overwrite a variable assignement with a function definition. You'd need to remove the variable assignement first.
simon, Thanx for the supporting fact.
2

There isn't necessarily a beneficial reason to use new Function() since you could simply create a blank function like so:

var f1 = function () { 
   // my function code here;
};

// calling the function
f1();

Each one will have the prototypes of Function attached to it either way.

new Function() (also new Array(), etc) should typically be avoided since it has to eval.

5 Comments

So is it a valid usage?
If valid please give a sample code to demonstrate the usage of it. That is what I actually needed.
var myObject = new f1(); (according to mcp's example)
Using that syntax how can I define a function. Would you please elaborate that in your answer???
They're not exactly equivalent; if you use a function expression like in your example, it will close over the outer scope, but new Function() won't.
1

Technically, there really isn't a difference between these two bits of JavaScript (other answers have covered this):

var f1 = new Function();

and

var f1 = function () { };

What you can do, however, is use Function to check against the types of your variables:

var f1 = 'cat';
console.log(typeof(f1) === typeof(Function)); // false

var f2 = function () { return 'cat'; };
console.log(typeof(f2) === typeof(Function)); // true

Comments

0

Function objects created with the Function constructor are parsed when the function is created. This is less efficient than declaring a function and calling it within your code, because functions declared with the function statement are parsed with the rest of the code.

from MDN

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.