zoukankan      html  css  js  c++  java
  • 闭包题目

    //题目一
      var name = "The Window";
      var object = {
        name: "My Object",
        getNameFunc: function () {
          return function () {
            return this.name;
          };
        }
      };
      console.log(object.getNameFunc()());  //直接调用,默认调用对象是window

    答案:the window

    题目二:

     //代码片段二
      var name2 = "The Window";
      var object2 = {
        name2: "My Object",
        getNameFunc: function () {
          var that = this;
          return function () {
            return that.name2;
          };
        }
      };
      console.log(object2.getNameFunc()()); //修改了this = that,说明this指的是getName

    答案:the Object

    题目三:

    /*
       说说它们的输出情况
       */
    
      function fun(n, o) {
        console.log(o)
        return {
          fun: function (m) {
            return fun(m, n)
          }
        }
      }
      var a = fun(0)
      a.fun(1);//fun(1,0)
      a.fun(2)
      a.fun(3) //undefined,?,?,?
    
      var b = fun(0).fun(1).fun(2).fun(3) //undefined,?,?,? 链式闭包
    //先执行fun(0)返回一个闭包.fun(1)==fun(1,0)
    //fun(1,0).fun(2)==fun(2,1)
    //fun(2,1).fun(3)==fun(3,2)
    var c = fun(0).fun(1)//fun(1,0) c.fun(2)//fun(2,1) c.fun(3) //undefined,?,?,?
    //答案:
    //a: undefined,0,0,0
    //b: undefined,0,1,2
    //c: undefined,0,1,1
    穷则独善其身,达则兼济天下……
  • 相关阅读:
    ngInclude与script加载模板
    ng-model-options
    angular模板加载 ----ng-template
    ngBind ngBindTemplate ngBindHtml
    ng-switch
    ng-show与ng-if区别
    运维教给我什么
    路漫漫其修远兮,吾将上下而求索
    那些让我们继续坚持下去句子
    随手记
  • 原文地址:https://www.cnblogs.com/hmy-666/p/14433256.html
Copyright © 2011-2022 走看看