The definition of JS function

Source: Internet
Author: User

The JS function can be defined in the following three ways

1. Define functions using the keyword function.

2, can be defined by the declaration.

3, it can also be an expression.

1. Keyword function definition (constructor)

var New Function (' A ', ' B ', ' return a * b '); var x = MyFunction (4, 3);

It is not necessary to actually use constructors (many times in JavaScript you need to avoid using the new keyword, and this approach can lead to parsing of two of code, affecting performance.) Parse the regular JavaScript code for the first time and parse the string passed in the constructor for the second time. The above example can be written as follows:

var function (A, B) {return A * B} var x = MyFunction (4, 3);

2, the declaration definition, in the manner of the expression than the parser will first read the function declaration and make it available (accessible) before executing any code, this is due to the JavaScript function declaration elevation;

function functionname () {    // execute code }

3, the expression method definition, namely the anonymous function, the expression definition must wait until the parser executes to its line of code, only then will actually be interpreted executes

var function (A, B) {return A * b};

Self-invoking function (actually a function that is called anonymously)
function expressions can "self-invoke". Self-invocation expressions are called automatically and cannot be called from a declared function.

(function() {    // Code }) ();;

The JS function is an object:

1, typeof function ();//return function.

2, the function has the property and the method.

3. The Arguments.length property returns the number of arguments received by the function call procedure.

function MyFunction (A, b) {    return  arguments.length;}

4, the ToString () method returns the function as a string.

function MyFunction (A, b) {    return A * b;} var txt = myfunction.tostring (); // function MyFunction (A, b) {return a * b}

The definition of JS function

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.