The invocation of anonymous function in JavaScript and the illustration of the example (various) _javascript skills

Source: Internet
Author: User
Tags anonymous

There are many ways to define functions in JavaScript, and the direct amount of the function is one of them. such as var fun = function () {}, where function does not assign value to fun then it is an anonymous function. OK, let's see how anonymous functions are invoked.

Mode 1, call the function, get the return value. The force operator makes the function call execute

(function (x,y) {
alert (x+y);
return x+y;

Mode 2, call the function, get the return value. Force the function to perform a direct amount of execution and return a reference, and then invoke the execution

(function (x,y) {
alert (x+y);
return x+y;

This is also the way many libraries love to call, such as Jquery,mootools.

Mode 3, using void

void function (x) {
x = x-1;
alert (x);
} (9);

Mode 4, using the-/+ operator

-function (x,y) {
alert (x+y);
return x+y;
} (3,4);
+function (x,y) {
alert (x+y);
return x+y;
} (3,4);
--function (x,y) {
alert (x+y);
return x+y;
} (3,4);
++function (x,y) {
alert (x+y);
return x+y;

Mode 5, using the tilde (~)

~function (x, y) {
alert (x+y);
return x+y;

Mode 6, anonymous function execution placed in brackets

[function () {
Console.log (this)/Browser console Output Window

Mode 7, anonymous function before adding typeof

typeof function () {
Console.log (this)/Browser console Output Window

Mode 8, anonymous function before adding delete

Delete function () {
Console.log (this)/Browser console Output Window

Mode 9, anonymous function before adding void

void function () {
Console.log (this)/Browser console Output Window

Mode 10, using the new method, the argument

New function (Win) {
Console.log (Win)//window

Mode 11, use new, do not pass the argument

New function () {
Console.log (this)//this is not window.

Mode 12, comma operator

 function () {
Console.log (this)//window

Mode 13, bitwise XOR OR operator

^function () {
Console.log (this)//window

Mode 14, comparison operator

function () {
Console.log (this)//window

Finally look at the wrong way to call

function (x,y) {
alert (x+y);
return x+y;

The n types of anonymous functions are written as follows

Anonymous functions have no actual names, no pointers, how do they execute?

About anonymous function writing, very divergent ~

The + number is to convert the function declaration to a function expression. Sum up

The most common usage:

The code is as follows:

(function () { 
alert (' Water '); 

Of course, you can also take parameters:

The code is as follows:

(function (o) { 
alert (o); 

Want to use a chained call to an anonymous function? Very simple:

The code is as follows:

(function (o) { 
console.log (o); 
return Arguments.callee; 

Common anonymous functions are known to look at uncommon:

The code is as follows:

~ (function () { 
alert (' Water '); 

The code is as follows:

void function () { 
alert (' Water '); 

The code is as follows:

+function () { 
alert (' Water '); 

The code is as follows:

-function () { 
alert (' water '); 
} ();

The code is as follows:

~function () { 
alert (' water '); 
} ();

The code is as follows:

!function () { 
alert (' Water '); 

The code is as follows:

(function () { 
alert (' water '); 
} ())//a bit of a compulsive taste ~

The above to share the JavaScript in the anonymous function of the call and write, I hope to help.

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.