【问题标题】:context in function in object method对象方法中的函数上下文
【发布时间】:2016-04-20 14:16:57
【问题描述】:

有一段代码:

var object = {
    findById: function(idNumber) {
        var data = this.childNodes;
        var returnItems = {};

        function callback(node) {
            if (parseInt(node.id) === idNumber)
                returnItems = node;
        };

        function iterator(node, callback) {
            callback(node);
            var nodes = node.childNodes;
            if (nodes === undefined) {
                return;
            };
            for (var i = 0; i < nodes.length; i++) {
                var iterNode = nodes[i];
                iterator(iterNode, callback);
            };
        };

        function bind(func, context) {
            return function() { // (*)
                return func.apply(context, arguments);
            };
        };

        for (var i = data.length - 1; i >= 0; i--) {
            iterator(data[i], callback);
        };

        return returnItems;
    },
}

如何将上下文导入迭代器和回调函数? 如果我将 console.log(this) 放入函数 iterator() - 这将是“窗口”,但不是我的对象。 它也不应该是 this.callback this.iterator 等。 据我了解,它应该像调用/应用或绑定。 怎么做?

【问题讨论】:

标签: javascript function callback this


【解决方案1】:
  1. findById 函数中复制对 this 的引用。

    var object = {
      findById: function(idNumber) {
        var data = this.childNodes;
        var returnItems = {};
    
        // assign this to a variable
        // you can use inside the nested functions
        var that = this;
    
    
        function callback(node) {
          if (parseInt(node.id) === idNumber)
            returnItems = node;
        };
    
        function iterator(node, callback) {
          callback(node);
          var nodes = node.childNodes;
          if (nodes === undefined) {
            return;
          };
          for (var i = 0; i < nodes.length; i++) {
            var iterNode = nodes[i];
            iterator(iterNode, callback);
          };
        };
    
        function bind(func, context) {
          return function() { // (*)
            return func.apply(context, arguments);
          };
        };
    
    
        for (var i = data.length - 1; i >= 0; i--) {
          iterator(data[i], callback);
        };
    
        return returnItems;
      }
    };
    
  2. 使用callapply

    for (var i = data.length - 1; i >= 0; i--) {
      iterator.call(this, data[i], callback);
    };
    

【讨论】:

    【解决方案2】:

    无论你在哪里使用函数都这样做:

    functionToCall.apply(this,params); //this or the context you want to have inside
    

    示例:

    function callable() {
       console.log(this);
    }
    callable(); //logs window
    callable.apply({}); //logs {}
    

    【讨论】:

    • 很难理解......它将如何在我的代码中工作? function callback.apply(this,node) { if (parseInt(node.id) === idNumber) returnItems = node; };??
    • 不不不,它不在定义上。当您调用该函数时。而不是调用 callback(node) 你调用它: callback.apply(this,node)
    猜你喜欢
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-19
    • 2011-02-07
    • 2012-07-04
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    相关资源
    最近更新 更多