function and function in JS

Source: Internet
Author: User

function is the identifier of JS

function is a constructor in JS

1. New function and new function difference

  The new operator in JS is an instance of creating a custom object or an instance of a local object with a constructor function.

Syntax: New constructor [([arguments])]

New function () {...} Initializes an actionable object, equivalent to the new function Anonymous () {...}

  

  The new function (' argument ', ' function body ') instantiates a function that is equivalent to the function name () {...}

  

2. Execute functions immediately (function () {...} ()) and (function () {...}) ()

//1.//define a named function A and call it, run normallyfunctionA () {}a ()//2.//2What about the way you use anonymous functions? function (){}() //This method will error: uncaught syntaxerror:unexpected token (//3.//according to the above-thought test, to find the cause of 2 errorfunction () {}//3will error, same as 2 error: uncaught syntaxerror:unexpected token (//4.();//4will error: uncaught syntaxerror:unexpected token)//5.(1);//5 No error, normal operation
6.
(function () {});
6 no error, normal operation

It can be seen that the reasons for the above phenomenon are: grammatical errors. The reason for this is that the () operator has precedence over the function call, and the () operator must have an expression inside the parentheses, or there will be a syntax error.

You can therefore define a self-invoking anonymous function in the form of (function () {}) ()

//1.(1+2) (5+6);//in the case of the above expression, an error will be made: Uncaught typeerror:3 is not a function, which means that if the first expression is a function, it will run normally. //2, so in the case of ensuring that the entire operation is an expression operation, the first subexpression is a function, the following wording is obtained(function(){}());//2 normal operation will not error//3, it can be concluded that the following wording is also correct!function(){};+function(){};-function(){};~function(){};......

function and function in JS

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.