1. 程式人生 > 資訊 >美聯社:美國政府啟動對特斯拉自動駕駛系統的正式調查

美聯社:美國政府啟動對特斯拉自動駕駛系統的正式調查

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        let a 
= 2; function biBao() { console.log(a) } function test() { let a = 1; return function () { console.log(a) } } biBao(); test()(); </script> </body> </html>

1、解釋

函式執行時,會形成一個私有上下文(詞法作用域),執行完之後,上下文中使用的物件,如果不在該上下文中定義,那麼當前上下文就不能被釋放,導致閉包

2、作用

  防止全域性變數汙染

  訪問函式內部變數

3、例項

避免變數汙染

      let n = 2;
        function test() {
            let n = 3;
            return function () {
                n++;
                console.log(n)
            }
        }
        let biBaoFunc 
= test(); biBaoFunc(); biBaoFunc(); biBaoFunc();

4、缺點

 佔記憶體

 可能會記憶體洩漏