Home > Web Front-end > JS Tutorial > Summary of basic JavaScript knowledge (10) Closures and immediate execution functions

Summary of basic JavaScript knowledge (10) Closures and immediate execution functions

php中世界最好的语言
Release: 2018-03-10 13:39:19
Original
1327 people have browsed it

This time I will bring you a basic knowledge summary of JavaScript. There are eleven knowledge points in total. Basic JavaScript knowledge summary (10) Closures and immediate execution functions. Here are practical cases. Let’s take a look. take a look.

//例子function a() {    function b(){        var bbb = 234;        console.log(aaa)
    }    var aaa = 123;    return b;
}var glob = 100;var demo = a();
demo();
Copy after login

First of all, after this function executes return b, function a has finished executing, and the AO of a will be destroyed. However, function b was saved before destruction, so the execution context of b was not destroyed. .

So b’s AO and Go are saved

This forms a closure, which can also be said: The internal function is saved to the outside, and it must be formed Closure

function a (){    var num  = 100;    function b(){
        num ++;        console.log(num);
    }    return b;
}var demo = a();
demo();//打印101demo();//打印102
Copy after login

The concept of closure: When the internal function is saved to the outside, a closure will be generated. Closure will cause the original scope chain not to be released, causing memory leaks (memory occupation)

Memory leaks: For example...a cup...the more water you put into it, the more The less memory you have (in the same way, the more memory you occupy, the less memory it has)

The role of closure

Implementing public variables

Function accumulator

Can be cached (storage structure)

eater

Can realize encapsulation and attribute privatization

Person()

Module Develop to prevent contamination of global variables

1. The above example of the accumulator has been written

2. Caching (storage structure) can be done

function test(){    var num = 100;    function a(){
        num ++;        console.log(num);
    };    function b(){
        num --;        console.log(num);
    };    return [a,b]
}var myArr = test();
myArr[0]();//打印101myArr[1]();//打印100
function eater(){    var food = "";    var obj ={        eat : function(){            console.log("i am eating" + food)
        },        push : function (myFood){
            food = myFood;
        }
    }    return obj;
}var eater1 = eater();
eater1.push('banana');
eater1.eat();//打印出 i am eating banana
Copy after login

The above For example, after eater1.push('banana'), I gave foot = "" in eater and changed it to food = "banana". After I executed eater1.eat(), I got food = "banana". They all operate on the same closure, so I am eating banana is printed. This foot is equivalent to an implicit storage structure. Same, this is a caching application

Buy a pit for yourself, privatize attributes, I will explain after I finish writingObject, the same is true for modular development

immediately Execution function

This type of function is not declared and is released after one execution. Suitable for initialization work.

function a(){
    
}function b(){
    
}
Copy after login

I define two functions. Unless my JavaScript is executed, these two functions will never be released. They will always wait to be executed, and they will take up space while waiting to be executed.

Function: Function for initialization function

(function (aaa,bbb,ccc){    var a = 234;    var b = 123;    var c = a+b;    console.log(c)    return c}(1,2,3))
执行完立即被销毁
// 怎么去接收他var num = (function (aaa,bbb,ccc){    var a = 234;    var b = 123;    var c = a+b;    console.log(c)    return c}(1,2,3))
立即执行函数有两种写法
//第一种(function(){}());//w3c 建议第一种//第二种(function(){})();
扩展
只有表达式才可以被执行符号执行
var test = function (){}()//也可以被执行+ function test(){}()//也可以被执行 以此类推- !号也可以function test(){    console.log(a+b+c+d)}(1,2,3,4)//不执行也不报错
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Related reading:

Summary of basic JavaScript knowledge (9) Scope and scope chain explanation

Basics Summary of JavaScript knowledge (8) Pre-compilation execution process

Summary of basic JavaScript knowledge (7) Recursion

The above is the detailed content of Summary of basic JavaScript knowledge (10) Closures and immediate execution functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template