尚学堂requireJs课程---1、作用域回顾

尚学堂requireJs课程---1、作用域回顾

一、总结

一句话总结:

尚学堂的课程的资料他的官网上面是有的

1、js作用域?

~ js中是函数作用域:局部变量的话要写var关键词
~ 闭包可以解决在外部读到局部变量的需求

2、闭包作用?

1)保存状态
2)读取局部变量

3、闭包怎么读取局部变量?

|||-begin

 1 function Demo(){
 2  var hello = "hello";
 3  return function(){
 4    // 闭包!!!
 5    // 
 6      console.log(hello);// 输出 "hello"
 7      return hello;
 8  }
 9 }
10 var hello_1=Demo()();
11 console.log(hello_1);// 输出 "hello"

|||-end

在外部通过闭包获取函数中变量的值

二、内容在总结中

 1 <!DOCTYPE html>
 2 <html lang="en" dir="ltr">
 3   <head>
 4     <meta charset="utf-8">
 5     <title></title>
 6   </head>
 7   <body>
 8 
 9 
10     <script>
11 
12       /*
13         作用域
14           js:函数级作用域
15           var关键字一定要写
16      */
17 
18      function demo01(){
19        var num = 19;
20        age = 20;
21      }
22 
23      demo01();
24      //console.log(num); //报错
25 
26 
27      function Demo(){
28        var hello = "hello";
29        return function(){
30          // 闭包!!!
31          // 
32          console.log(hello);// 输出 "hello"
33          return hello;
34        }
35      }
36      var hello_1=Demo()();
37      console.log(hello_1);// 输出 "hello"
38 
39       </script>
40 
41   </body>
42 </html>
 
原文地址:https://www.cnblogs.com/Renyi-Fan/p/11610379.html