[JavaScript] +function () {} function

Source: Internet
Author: User

[JavaScript] +function () {} function

Gab
In JS, often encounter the following code, exactly in the function before adding a unary operator, what role?
The function is to turn the functions into self-executing functions

+function   ()  {} () ;   

The plus sign here can be substituted for ~ , * , + , - , ! other unary operators,

The effect is equivalent to:

 (function  ()  { Span class= "hljs-built_in" style= "color: #268bd2;" >console . Log ( "foo!" ); })(); //or  (function  ()  {console . Log ( "foo!" ); }());

Without this plus, the parser will assume that function is the beginning of a declaration of functions, and the latter () will result in a syntax error.
With the + sign in front of the function, it becomes a functional expression, and a function expression adds another () to become a function that executes immediately.

[JavaScript] +function () {} 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.