CONSTRUCTOR FUNCTION

CONSTRUCTOR FUNCTION :

The function statement is not the only way to define a new function but also, you can define your function dynamically using Function( ) constructor along with the new operator.

Syntax:

<script type="text/javascript">
var variablename = new Function(Arg1, Arg2..., "Function Body");
</script>

 Description:

  • The Function() constructor expects any number of string arguments.
  • The last argument is the body of the function - it can contain arbitrary JavaScript statements, separated from each other by semicolons.
  • a function declaration is not an executable statement, it is not common to end it with a semicolon.
  • Declared functions are not executed immediately. They are "saved for later use", and will be executed later, when they are invoked (called upon).

anonymous functions:(a function without a name)

The unnamed functions created with the Function() constructor are called anonymous functions.

Example:

<script type="text/javascript">
 var func = new Function("x", "y", "return x*y;");
 </script>



Reference from: http://www.tutorialspoint.com/javascript

No comments:

Post a Comment