Featured image of post 【JavaScript 函式】Closure 闭包

【JavaScript 函式】Closure 闭包

【JavaScript 函式】Closure 闭包

Photo by Mohammad Rahmani on Unsplash

闭包是个 捕捉了外部函式变数(或使之继续存活)的函式

const foo = () => {
  let name = 'hi';

  // closure 闭包函式
  const bar = () => {
    // 使用外层变数
    // KJ
    console.log(name);
  }

  bar();
}

foo();

基本上函式离开执行环境时,也会同时将佔用的记忆体空间给释放出来

例如以上 name 变数 应该在执行完毕就会在 memory (记忆体) 中被清掉。

但因为 closure 特性此 name 变数 还会继续被保留

运用 closure 特性让变数可以一直被保留并且做运算

var storyWriter = function(){
    var story = "";

    var addWords = function(string){
        story = story + string
        return story.trim();
    }

    var erase = function(){
        story = "";
    }
    return {
        addWords: addWords,
        erase: erase
    }
}

let writer = storyWriter();

writer.addWords('444');
writer.addWords('555');
let words = writer.addWords('666');
// 444555666
console.log(words);

取用变数范围

const globalVariable = `global variable`;
const outerFunc = (outerParam) => {
    const outerVariable = `outer variable`;

    let innerFunc = (innerParam) => {
        const innerVariable = `inner variable`;
        // 可取用全域变数
        console.log(`[inner] globalVariable: ${globalVariable}`)
        // 可取用外层变数
        console.log(`[inner] outerVariable: ${outerVariable}`)
        // 可取用本身变数
        console.log(`[inner] innerVariable: ${innerVariable}`)
        // 可取用外层参数
        console.log(`[inner] outerParam: ${outerParam}`);
        // 可取用本身参数
        console.log(`[inner] innerParam: ${innerParam}`);
    }
    // 呼叫内部函式
    innerFunc(`inner param`);

    // 可取用全域变数
    console.log(`<outer> globalVariable: ${globalVariable}`)
    // 可取用本身变数
    console.log(`<outer> outerVariable: ${outerVariable}`)
    // 可取用本身参数
    console.log(`<outer> outerParam: ${outerParam}`);

}
outerFunc(`outer param`);

// [inner] globalVariable: global variable
// [inner] outerVariable: outer variable
// [inner] innerVariable: inner variable
// [inner] outerParam: outer param
// [inner] innerParam: inner param
// <outer> globalVariable: global variable
// <outer> outerVariable: outer variable
// <outer> outerParam: outer param

闭包内的 this 变数

var myCar = {
    color: `Blue`,
    logColor: function() {
        var self = this;
        // In logColor — this.color: Blue
        console.log(`In logColor — this.color: ` + this.color);
        // In logColor — self.color: Blue
        console.log(`In logColor — self.color: ` + self.color);
        (function() {
            // In IIFE — this.color: undefined
            console.log(`In IIFE — this.color: ` + this.color);
            // In IIFE — self.color: Blue
            console.log(`In IIFE — self.color: ` + self.color)}
        )()
    },
}
myCar.logColor();

箭头函式闭包内的 this 变数

箭头函式当中的 this 绑定的是是定义时的物件,而不是使用时的物件,所以定义当下 this 指的是哪个物件就会是那个物件,不会随着时间改变

所以闭包内的还是能够存取到 this.color 的资料为定义他的 myCar.color = Blue

var myCar = {
    color: `Blue`,
    logColor: function() {
        var self = this;
        // In logColor — this.color: Blue
        console.log(`In logColor — this.color: ` + this.color);
        // In logColor — self.color: Blue
        console.log(`In logColor — self.color: ` + self.color);
        (() => {
            // In IIFE — this.color: Blue
            console.log(`In IIFE — this.color: ` + this.color);
            // In IIFE — self.color: Blue
            console.log(`In IIFE — self.color: ` + self.color)}
        )()
    },
}
myCar.logColor();

参考资料

Donate KJ 贊助作者喝咖啡

如果這篇文章對你有幫助的話,可以透過下面支付方式贊助作者喝咖啡,如果有什麼建議或想說的話可以贊助並留言給我
If this article has been helpful to you, you can support the author by treating them to a coffee through the payment options below. If you have any suggestions or comments, feel free to sponsor and leave a message for me!
方式 Method 贊助 Donate
PayPal https://paypal.me/kejyun
綠界 ECPay https://p.ecpay.com.tw/AC218F1
歐付寶 OPay https://payment.opay.tw/Broadcaster/Donate/BD2BD896029F2155041C8C8FAED3A6F8
All rights reserved,未經允許不得隨意轉載
Built with Hugo
主题 StackJimmy 设计