1

The caption says it all.

I am reading a book and in the Function chapters, the author puts forward that there are advantages of declaring a function using the new operator.

But then I have seen the declaration like

function myFunction(){
}

more often in the applications, than declaring it like,

var myFunction = new Function();

Any suggestions?

Farhan Shirgill Ansari
  • 13,172
  • 7
  • 49
  • 95
  • I think this is duplicate of this http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript – Muhammad Usman Sep 04 '15 at 06:32
  • 2
    @MuhammadUsman - no, this question is asking about `new Function(...)` that creates a new function object, not `new SomeConstructor()` that creates a new regular object. – jfriend00 Sep 04 '15 at 06:34

1 Answers1

3

From the MDN description of Function:

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

All arguments passed to the function are treated as the names of the identifiers of the parameters in the function to be created, in the order in which they are passed.

Note: Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was called. This is different from using eval with code for a function expression.

So, from this, you see the following differences when using new Function(...):

  1. The code for the new function is a string and is parsed when the new Function(...) is called, not when the page is parsed. For example, you can build a string and then use that as your function body.

  2. These functions are always created in the global scope and do not create closures within the environment they are created in.

  3. These functions cannot access parent scoped variables.

  4. You can dynamically construct code and then have it turned into a parsed/live function - somewhat similar to eval(), but with a few subtle differences.


Note: Because the function body with the new Function(...) construct has to be contained in a Javascript string, it's messy to write most code this way. new Function(...) should generally only be used for special circumstances where you need to construct a function body on the fly (which is likely a very rare case) and not where you know the code ahead of time.

jfriend00
  • 580,699
  • 78
  • 809
  • 825
  • Upvoted, but far bottom in the same chapter, the author suggests "advantage of declaring a function using the new operator is that a script can create a function after a document loads." I think I didn't get it. Can you please elaborate on this. – Farhan Shirgill Ansari Sep 04 '15 at 06:43
  • 1
    @ShirgillAnsari - that's my point #1 and #4 above. It is very rare when this would be needed. The only circumstance I've seen is when user supplied code is dynamically added to a page. I know of no reason to use this construct for your own code. – jfriend00 Sep 04 '15 at 07:05