【问题标题】:Javascript object created from objectss always references last element从对象创建的 Javascript 对象总是引用最后一个元素
【发布时间】:2012-04-05 18:06:42
【问题描述】:

这是一种奇怪的问题。鉴于下面的 javascript,我希望 newFunctions 对象包含包装原始对象的函数,但它只运行循环中发生的最后一个操作。 var actionToCall 不应该复制对 currentAction 当前正在查看的内容的引用,并且在循环迭代时不更改?我被难住了。

  var typeArray = {
    type1: {
      function1: function() {
        console.log("this is function 1");
      },
      function2: function() {
        console.log("this is function 2");
      }
    },
    type2: {
      somefunction: function() {
        console.log("this is some function")
      }
    },
    type3: {
      blah: function() {
        return;
      },
      moreBlah: function(something) {
        console.log(something);
      },
      evenMore: function() {
        console.log("I'm last!");
      }
    }
  },
      index,
      typeIndex,
      currentType,
      actionIndex,
      currentAction,
      newFunctions = {};

  for(typeIndex in typeArray) {
    currentType = typeArray[typeIndex];
    for(actionIndex in currentType) {
      currentAction = currentType[actionIndex];

      console.log(currentAction.toString());

      newFunctions[actionIndex] = function() {
        var actionToCall = currentAction;

        console.log("about to run action");

        actionToCall.call(this);

        console.log("action has been called");
      }
    }
  }

  console.log(newFunctions);

  for(index in newFunctions) {
    (newFunctions[index])();
  }

【问题讨论】:

    标签: javascript object iteration


    【解决方案1】:

    这是因为 actionToCall 被分配给 currentAction。

    由于 currentAction 是全局的,它的值会随着循环的迭代而不断变化。

    当循环结束时,currentAction 被赋值给 evenMore。

    这是使用自执行函数来诱导作用域的修复方法。

    var typeArray = {
        type1: {
            function1: function() {
                console.log("this is function 1");
            },
            function2: function() {
                console.log("this is function 2");
            }
        },
        type2: {
            somefunction: function() {
                console.log("this is some function")
            }
        },
        type3: {
            blah: function() {
                return;
            },
            moreBlah: function(something) {
                console.log(something);
            },
            evenMore: function() {
                console.log("I'm last!");
            }
        }
    },
    index,
    typeIndex,
    currentType,
    actionIndex,
    currentAction,
    newFunctions = {};
    
    for(typeIndex in typeArray) {
        currentType = typeArray[typeIndex];
        for(actionIndex in currentType) {
            currentAction = currentType[actionIndex];
    
            console.log(currentAction.toString());
    
            //induce scope here so actionToCall keeps the current value of currentAction.
            (function(){
    
                var actionToCall = currentAction;
                newFunctions[actionIndex] = function() {
    
    
                    console.log("about to run action");
    
                    actionToCall.call(this);
    
                    console.log("action has been called");
                }
            })();
        }
    }
    
    console.log(newFunctions);
    
    for(index in newFunctions) {
        (newFunctions[index])();
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-21
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      相关资源
      最近更新 更多