The principle of JavaScript self-executing anonymous function (function () {}) _javascript skills

Source: Internet
Author: User

A function is one of the most flexible objects in JavaScript, and it simply explains the purpose of its anonymous function. An anonymous function is a function that does not specify a function name or pointer, and the execution of an anonymous function is only one of them, which is called the function: self-execution function

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

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

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

What prompted it to automate? , look at the following code

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

There is no difference between these and the traditional ways of doing it,

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

operator + passed to the native parameter = function auto execution? But some symbols also do not support, such as "=,*,/", the reason for its implementation is still very mysterious, the Internet can not find a decent answer

And then I discovered a magical phenomenon where these operators can stack infinitely ...

function is preceded by a specific symbol can be infinite superposition ...
~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~ !+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~!+-~!+-+-!~~~~~~~~+-!!!!!! +function A (b) {
alert (' Hello ');
} ();

The program is running normally!!!!!!

However, there are two cases of error

If there are three or more consecutive "+" or "-" symbols, there will be an error;

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

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

Then I use the same notation to compute a variable and find the exact same one, which may already indicate that the JavaScript operation prompts the function to execute automatically, or it can be understood to call this function by operation!

The function is not executed by itself, but by an operation. , but only partial operation mode is supported!

In addition, this self execution function is not necessarily an anonymous function! Looking at the top of the code, I defined the function name a in the function after the operator, and nothing unusual, but also no eggs-!, this a is still unable to be called by other methods, but I think a lot of people call it the anonymous function is a bit inappropriate!

The above is a small set to introduce the JavaScript from the implementation of the anonymous function (function () {}) of the principle of analysis, I hope to help you!

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.