• 闭包


    闭包:
      应用两种情况
       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

  • 相关阅读:
    面向对象之魔术方法
    装饰器和单例模式练习
    装饰器练习
    扩展数据类型练习
    Vlan的 tag 和 untagged
    【转】OSI 七层模型和TCP/IP模型及对应协议(详解)
    性能测试的相关概念和指标。
    Python接口自动化之数据驱动
    Python接口自动化之登录接口测试
    Python接口自动化之unittest单元测试
  • 原文地址:https://www.cnblogs.com/eye-color/p/6863662.html
Copyright © 2020-2023  润新知