Analysis of the principle of JavaScript self-executing anonymous function (function () {}) ()

Source: Internet
Author: User

Anonymous functions are functions that do not have a function name. This article mainly introduces JavaScript self-executing anonymous function (function () {}) The principle of the analysis of the relevant information, the need for friends can refer to the following

A function is the most flexible object in JavaScript, and this is just about the purpose of its anonymous function. An anonymous function is a function that does not specify a function name or pointer, and the self-executing anonymous function is just one of these functions: self-executing function

The following is one of the most common self-executing functions:

Traditional anonymous function (function () {alert (' Hello ');}) ();

The result of this code is that it pops up when the page is reloaded: "Hello"

What motivates it to execute automatically? , take a look at the following code

Remove the parentheses in the traditional notation and precede the operator ~,!, +,-~function () {alert (' Hello ');} ();! function () {alert (' Hello ');} (); +function () {alert (' Hello ');} ();-function () {alert (' Hello ');} ();

  

These formulations are no different from the traditional way of doing what is said above,

I find that the common denominator of these formulations is the operator, in fact, the traditional way of parentheses () is also a kind of operation, appears in: a=b* (c+d),

operator + arguments passed to self = function is executed automatically? But some symbols also do not support, such as "=,*,/", which is the reason for its self-implementation is still very mysterious, the Internet can not find a decent answer

Then I discovered a magical phenomenon in which these operators can overlay infinitely ...

Function front is a specific symbol can be superimposed indefinitely ... ~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+- +-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~~~~~~~~+-!!!!!! +function A (b) {alert (' Hello ');} ();

  

The program runs correctly!!!!!!

However, there are two cases of error

If there are three or more consecutive "+" or "-" symbols, then an error occurs;

If two consecutive "+" or "-" symbols are followed by other symbols, an error occurs, such as "++~", "--+";

Error hint: uncaught referenceerror:invalid left-hand side expression in prefix operation (meaning left expression error)

Then I use the same symbol to compute a variable and find exactly the same, which may already indicate that the JavaScript operation is prompting the function to execute automatically, or it can be understood to call this function by operation!

It is not the function that executes itself, but the function called by the operation! , but only part of the operation mode is supported!

In addition, this self-executing function is not necessarily an anonymous function! Looking at the code above, I define the function name a in the function behind the operator, there is nothing unusual, but there is no egg--!, this a still can't be called by other methods, but I think a lot of people call it anonymous function is a bit inappropriate!

The above is a small part of the introduction of JavaScript self-executing anonymous function (function () {}) of the principle of the analysis, I hope to help you!

Analysis of the principle of JavaScript self-executing anonymous 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.