Obfuscation about javascript function objects

Source: Internet
Author: User

 

Function objects in js are fascinating, but they are often confusing because they are too flexible. I will post some code below.

 

Most people are abbreviated as follows:

 

Function test (){}

The entire book jsvascript is written as follows:

 

Var test = function (){}

 

Functions can be run immediately and assigned values:

 

Var test = function () {} () // test = undefined

Var test2 = function () {return 'sugar browsed '} () // test2 = 'sugar browsed'

 

However, function abbreviations cannot be run directly. The following code reports an error:

 

Function test () {} () // SyntaxError: syntax error

 

If the package is normal after the "()" operator is used:

 

(Function test (){})();

 

In fact, this function name "test" is meaningless. After it is removed, it becomes an anonymous function, and the code in the function body can still be automatically executed. Commonly used anonymous function Syntax:

 

(Function (){})();

 

The write of anonymous functions may also be "beautiful:

 

(Function (){}());

 

From the tmaic Column

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.