This article describes the syntax of the Function object in JavaScript through examples. The Function object in JavaScript is to create a new Function.
Function objects in JavaScript
In JavaScript, the Function object is used to create a new Function.
The usage is as follows:
Syntax 1
- functionfunctionName([argname1[,...[,argnameN]]])
- {
- body
- }
Syntax 2
Reference:
- functionName=newFunction([argname1,[...argnameN,]]body);
-
FunctionName is required. Name of the newly created Function
Argname1.. argnameN is optional. List of parameters received by the function.
Body is optional. A string that contains the JScript code block executed when the function is called.
The basic data type in the function JScript. Syntax 1 creates a Function value that is converted from JScript to a Function object when necessary. When calling a function, JScript converts the function created in syntax 2 to a Fnction object.
Syntax 1 is the basic method for creating functions in JScript. Syntax 2 is another method used to explicitly create function objects.
For example, to create a function that adds the two parameters passed to it, you can use either of the following methods:
Example 1
- Functionadd (x, y)
- {
- Return (x + y); // execute addition and return the result.
- }
Example 2
- varadd=newFunction("x","y","return(x+y)");
-
In either case, you can use the following code line to call the function:
- add(2,3);
-
When calling a function, make sure that the brackets and necessary parameters are included. When calling a function, no parentheses are used to return the text of the function rather than the result of function execution.