• 闭包


    闭包:
      应用两种情况
       1、函数作为返回值
       2、函数作为参数传递

          1、
              function fn1(){
          var max = 10;
          return function bar(x){
            if(x > max){
            console.log(x)
            }
          }
        }
        var f1 = fn1();
        f1(15);

       2、
        var max = 10,
        fn2 = function (x) {
          if(x > max){
          console.log(x) //15
          }
        };
        (function (f) {
          var max = 100;
          f(15)
        })(fn2)

       var name = "The Window";
        var object = {
          name : "My Object",
          getNameFunc : function(){
            return function(){
              return this.name;
            };
          }
        };
      console.log(object.getNameFunc()());//The Window

      var name = "The Window";
      var object = {
        name : "My Object",
        getNameFunc : function(){
          var that = this;
          return function(){
            return that.name;
          };
        }
      };
    console.log(object.getNameFunc()());//My Object

  • 相关阅读:
    学习嵌入式规划
    函数指针与指针函数
    css3
    css
    file upload使用iframe
    上传图片,文件
    table td里面的内容多的话出...
    html5 新标签
    html element
    css/html规范
  • 原文地址:https://www.cnblogs.com/eye-color/p/6863662.html
Copyright © 2020-2023  润新知