Function() ?

Function() :
Functions are actually objects. There is a built-in constructor function called Function() which allows an alternative way to create a function.

The following three ways of defining a function are equivalent:

>>> function sum(a, b) {return a + b;};
>>> sum(1, 2)
3
>>> var sum = function(a, b) {return a + b;};
>>> sum(1, 2)
3
>>> var sum = new Function('a', 'b', 'return a + b;');
>>> sum(1, 2)
3

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 then needs to evaluate the source code you pass and create the new function for you. If you use the Function constructor to create functions that have lots of parameters,bear in mind that the parameters can be passed as a single comma-delimited list, so,for example:

>>> 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]

More Info : Function

About

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Javascript Tagged with: