Differences between functions defined by Javascript

Source: Internet
Author: User

We all know that Javascript has two Function-defining methods that are very common. For example

Function a () {alert ("a")} var a = function () {alert ("")}

In javascript, any Function is a Function object. One is called implicit creation and the other is display creation,

What is "display creation" is to write a keyword "NEW". (This is not a scientific understanding.) For example:

Var test = new Function (cs1, cs2 .... CsN, function-body)

Note that cs1, cs2.. indicates the parameter, and function-body indicates the function body. You can call this function by using the variable name of the function object. You can call test (cs1, cs2…) in this way ...), Or assign a value to a variable, which can be called.

Var test = new Function ("cs1", "cs2", "alert (cs1 + cs2)"); test (1, 2 );

The effect is equivalent to our implicit creation.

Function test (cs1, cs2) {alert (cs1 + cs2);} test (1, 2) // var test1 = test; // test1 (1, 2 );

Therefore, when you use an implicit object, you must consider that it is a Function object created using the new keyword,

Alert (test. toString ());

You can output the code of the function body.

In addition, we usually use an anonymous function to create a function.

Var test = function (cs1, cs2) {alert (cs1 + cs2);} test (1, 2 );

The effect is equivalent to the creation of the display. Since the created function variable is an object, we can add new attributes and methods to the variable, in our js, the method is a special attribute, which requires attention.

Test. love = "I love php"; alert (test. love); test. php = function (msg) {alert (msg);} test. php ("how to study php! ");

 

Related Article

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.