Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

preface

After the last chapter, we are going to move on to the next one

closure

The concept of closures

The concept of a closure is not complicated, but its definition is somewhat convoluted:

  • Closure: A Closure is formed by binding a function with references to its surrounding state (lexical context).

    • You can call an inner function of another function in another scope and access members of the scope within that function

    Example:

    function makeFn () {
        let msg = 'hello world'
        return function () {
            console.log(msg)
        }
    }
    const fn = makeFn()
    fn()
    Copy the code

The nature of closures

Functions are put on an execution stack when they’re executed and then removed from the execution line when they’re done; However, scoped members on the heap cannot be freed because they are referenced externally, so the inner function can still access the members of the outer function.

For example

Functions are put on an execution stack when they’re executed and then removed from the execution line when they’re done
function add () {
    let num = 1
    num = num + 1
    console.log(num)
}
const fn = add

fn() / / 2
fn() / / 2
fn() / / 2
Copy the code

Every time we execute this code, num is reset to 1 again, and so on

Scope members on the heap cannot be freed because they are referenced externally
function add () {
    let num = 1
    return function () {
        num = num + 1
        console.log(num)
    }
}
const fn = add()

fn() / / 2
fn() / / 3
fn() / / 4
Copy the code

So the inner function can still access the members of the outer function.

Pros and cons of closures

Advantages of closures:

  1. The ability to read variables inside functions
  2. Keep these variables in memory and not be collected by the garbage collection mechanism after the call

Disadvantages of closures:

As the saying goes, too much of a thing will have a negative effect. Closures can cause variables in functions to be stored in memory, which consumes a lot of memory. Therefore, closures should not be abused.

The case of closures

Find the multiple square function

demand

Find the power of a number

implementation
function makePower (power) {
    return function(number){
        return Math.pow(number,power)
    }
}
/ / square
let power2 = makePower(2)
/ / cubic
let power3 = makePower(3)

// 4 squared
console.log(power2(4)) / / 16
// 5 squared
console.log(power2(5)) / / 25
// 4 to the third power
console.log(power3(4)) / / 64
Copy the code

Ask for employee salary

Requirements:
- Employee salary is based on performance salary + base salary - the same base salary for employees at the same levelCopy the code
implementation
function makeSaleary (base) {
    return function (performace) {
        return base + performace
    }
}
let base1 = makeSaleary(2000)
let base2 = makeSaleary(4000)

console.log(base1(10000)) / / 12000
console.log(base1(12000)) / / 14000
console.log(base2(10000)) / / 14000
console.log(base2(12000)) / / 16000
Copy the code

conclusion

Through this chapter we understand the concept of closure, advantages and disadvantages, use, like the students for attention, for comment, cheer each other!