JavaScript anonymous function Instance Analysis _javascript skill

Source: Internet
Author: User
Tags anonymous function definition

The examples in this article describe the use of JavaScript anonymous functions. Share to everyone for your reference. The specific analysis is as follows:

Summary:

This article is about the most basic JavaScript is the most important thing-function, the reason for writing this article, because of the interview when asked, is also known as a new temperature.

The first example, if you read it, means you have understood what this article is about.

Copy Code code as follows:
var f = (function () {

function f () {return 10;}

return f ();

function f () {return 20;}

var f = 30;

})();

Console.log (f);

This describes functions in JavaScript advanced programming--you can encapsulate any number of statements, and you can invoke execution anywhere, anytime. Strict mode has been introduced before, and strict modes have some limitations on functions:

① cannot name a function eval or arguments
② cannot name the parameter eval or arguments
③ cannot have two named parameters with the same name

This can cause syntax errors and the code cannot be executed.

function definition

The function definition is divided into three kinds

1. Constructor function

Copy Code code as follows:
var fun = new Funciton ();

2. General definition

Copy Code code as follows:
function Fun () {}

3. Functional definition

Copy Code code as follows:
var fun = function () {};

In all three ways, you can define the function fun.

Parameters

The function does not mind passing in the number of arguments, nor does it care what data type the incoming parameter is. Even if you define a function that receives only two parameters, it may not be necessary to pass two arguments when calling this function. You can pass one, three, or even no parameters. The reason is that the parameters are internally represented by an array. A parameter array can be accessed by arguments objects in the function body, for example

Copy Code code as follows:
function Sayhi () {

Alert ("Hello" + arguments[0] + "," + arguments[1]);

}

You can learn how many parameters are available by accessing the length property of the arguments object. The length of the function returns the number of arguments for the function.

Note: All parameters pass a value and it is not possible to pass arguments by reference.

function cannot be overloaded, can only override

If two functions with the same name are defined, the name only belongs to the last defined function, for example:

Copy Code code as follows:

function Add (num) {

return num + 100;

}

function Add (num) {

return num + 200;

}

var result = Add (100)//300

Note: The function stops and exits immediately after executing the return statement.

function type

The function is divided into two types, one is a well-known function, the other is an anonymous function. For example, the following well-known function

Copy Code code as follows:
function Fun () {

}

If you call, you just need to fun ().

Anonymous functions, as the name implies that there is no function name. For example

function () {}

function calls are called by function names, how do anonymous functions call? One is to assign an anonymous function to a variable, and let the variable act as the function name. The other is to use () to invoke, for example, the following three methods

1, (function () {return;} ());

2, (function () {return;}) ();

3, function () {return;} ();

Example:

Copy Code code as follows:

(function (x, y) {

Alert (x + y);

}) (2,3);

Alert (5)


2 and 3 will be passed as arguments to X and Y

Let's talk about the top example, which involves closures, and it says

First you define a variable F, and then you assign an anonymous function, and here you need to be aware that the definition of all the variables in the function will be preset, so the order of execution in the anonymous function is

Copy Code code as follows:

var f = (function () {

var f = 30;

function f () {return 10;}

function f () {return 20;}

return f ();

})();


The outside variable f and the inside variable F are not in the same scope (closures), so do not affect each other. Because the function cannot be overloaded, the outer variable f= (function f () {return 20;}) ();, so the final output is 20.

I hope this article will help you with your JavaScript programming.

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.