0

while searching around the internet i found two ways of writing javascript function.

One is Function() constructor

var myFunction = new Function("a", "b", "return a * b");

var x = myFunction(4, 3);

Second is simple defining function

var myFunction = function (a, b) {return a * b};

var x = myFunction(4, 3);

When i used above both method i found no difference between these 2.

Is there any difference between these 2 or is there any use of using function constructor??

Gaurav Aggarwal
  • 9,809
  • 6
  • 36
  • 74
  • The first form will not have access to any locally-scoped variables. Also, it is less efficient, because it has to be parsed at run-time. In general, there is no reason to use it. –  May 10 '16 at 06:46
  • First one is converting an arbitrary string into an executable function. Not secure unless string is already validated. – gurvinder372 May 10 '16 at 06:47
  • 1
    And you could have searched for that, couldn't you? At 3000+ reputation I would actually expect that you search first. – Tomalak May 10 '16 at 06:52
  • @Tomalak i suggest you to read the question carefully...the question you marked as duplicate is about declaring a function into a variable..and i asked about difference between declaring a function simply and with `new Function()` constructor...read question carefully before marking as duplicate – Gaurav Aggarwal May 10 '16 at 06:54
  • `new Function()` can be used to pass the function's body in a string. And hence this can be used to create dynamic functions. Also passing the script without executing the script. – SuperNova May 10 '16 at 06:54
  • @Trying2Learn is there any relevant example for your statement.. – Gaurav Aggarwal May 10 '16 at 06:55
  • @GauravAggarwal plz go to this link stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname/37131496#37131496. Hope this helps. – SuperNova May 10 '16 at 07:06
  • I don't understand why you all are relating this question with http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname this question that us different thing about parsing a function in variable but i am here talking about Function() constructor. – Gaurav Aggarwal May 10 '16 at 07:18
  • I resist the temptation to search a duplicate question (or - gasp - the documentation) for what `new Function()` means. – Tomalak May 10 '16 at 08:19
  • 1
    Reopened, since the proposed duplicate had nothing to do with the question. This does not mean this question _isn't_ a duplicate, it was just not an appropriate target. – Xan Jul 07 '16 at 14:04

1 Answers1

2

Object-Oriented JavaScript - Second Edition: When using the Function() constructor, you pass the parameter names first (as strings) and then the source code for the body of the function (again as a string). The JavaScript engine needs to evaluate the source code you pass and create the new function for you. This source code evaluation suffers from the same drawbacks as the eval() function, so defining functions using the Function() constructor should be avoided when possible.

 var first = new Function(
     'a, b, c, d',
     'return arguments;'
 );

 first(1, 2, 3, 4); // [1, 2, 3, 4]

 var second = new Function(
     'a, b, c',
     'd',
     'return arguments;'
 );

 second(1, 2, 3, 4); // [1, 2, 3, 4]

 var third = new Function(
     'a',
     'b',
     'c',
     'd',
     'return arguments;'
 );

 third(1, 2, 3, 4); // [1, 2, 3, 4]

Best practice: Do not use the Function() constructor. As with eval() and setTimeout(), always try to stay away from passing JavaScript code as a string.

What is the difference? See @Greg's answer

Community
  • 1
  • 1
Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47