Javascript anonymous function instance analysis

  • 2020-03-30 04:21:52
  • OfStack

This article illustrates the use of javascript anonymous functions. Share with you for your reference. Specific analysis is as follows:

Abstract:

This article is about the most basic and important thing of javascript -- functions. I wrote this article because I asked it in the interview, so I learned something new by reviewing the past.

Let's start with the last example. If you get it, you've got it.

var f = (function() {
    function f() {return 10;}
    return f();
    function f() {return 20;}
    var f = 30;
})();
console.log(f);

In advanced javascript programming, functions are described this way -- they can encapsulate as many statements as you want, and they can be called and executed anywhere and anytime. Strict mode was introduced earlier. Strict mode has some restrictions on functions:

You cannot call a function eval or arguments
The argument cannot be named eval or arguments
Cannot appear two named parameters of the same name

This can lead to syntax errors and unexecutable code.

The function definitions

There are three kinds of function definitions

1. Constructor

var fun = new Funciton();

2. Common definitions

function fun() {}

3. Functional definition

var fun = function() {};

You can define the function fun in all three ways.

parameter

The function doesn't care how many arguments are passed in, nor what data type is passed in. Even if you define a function that takes only two arguments, you do not have to pass two arguments when calling the function. You can pass one, three, or no arguments. The reason is that the arguments are represented internally in an array. In the body of the function you can access the arguments array through the arguments object, for example

function sayHi() {
    alert("Hello " + arguments[0] + "," + arguments[1]);
}

You know how many arguments there are by accessing the length property of the arguments object. The length of the function returns the number of arguments to the function.

Note: all parameters are passed as values. It is not possible to pass parameters by reference.

Functions cannot be overloaded, they can only be overridden

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


function add(num) {     return num + 100; } function add(num) {     return num + 200; } var result = add(100) //300

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

The function type

There are two kinds of functions: one is named, the other is anonymous. For example, the following named function

function fun() {
}

If called, all you need is fun().

Anonymous functions, by definition, have no function name. For example,

The function () {}

Function calls are called by the function name, how do you call an anonymous function? One is to assign an anonymous function to a variable that ACTS as the function name. The other is to call it with (), for example, the following three methods

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

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

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

Example:


(function(x, y) {     ​alert(x + y); })(2,3); //alert(5)

2 and 3 will be passed to x and y as arguments

Let's move on to the top example, which involves closures, which we'll talk about later

I'm going to define a variable f, and then I'm going to assign an anonymous function, and it's important to note that all the variables in the function are prefixed, so the order of execution in the anonymous function is


var f = (function() {     ​var f = 30;     function f() {return 10;}     function f() {return 20;}     return f(); })();

The outer variable f is not in the same scope as the inner variable f (closure), so it does not affect each other. The & # 8203; Since the function cannot be overloaded, the outer variable f=(function f() {return 20; }) (); So the final output is 20.


Related articles: