Deep Learning of Function object syntax in JavaScript

Source: Internet
Author: User

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

 
 
  1. functionfunctionName([argname1[,...[,argnameN]]])  
  2. {  
  3. body  

Syntax 2

Reference:

 
 
  1. functionName=newFunction([argname1,[...argnameN,]]body);  
  2.  

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

 
 
  1. Functionadd (x, y)
  2. {
  3. Return (x + y); // execute addition and return the result.
  4. }

Example 2

 
 
  1. varadd=newFunction("x","y","return(x+y)");  
  2.  

In either case, you can use the following code line to call the function:

 
 
  1. add(2,3);  
  2.  

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.