2016-04-20 8 views
0

Es ist Stück Code:Kontext in Funktion in Objektmethode

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; 
    }, 
} 

Wie in importieren Kontextfunktion iterator und Rückruf? wenn ich console.log (this) in die Funktion iterator() setze - dies wird 'window' sein, aber nicht mein Objekt. Auch sollte es nicht this.callback this.iterator usw. sein. Wie ich verstehe, sollte es wie Anruf/Anwenden oder binden sein. Wie geht das?

+0

Ich denke .proxy $ ist das, was Sie suchen http://stackoverflow.com/questions/4986329/understanding-proxy-in-jquery – Failwyn

+1

[Function.prototype.call] (https: //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) [Funktion.prototype.apply] (https://developer.mozilla.org/en-US/docs/ Web/JavaScript/Referenz/Global_Objects/Function/apply) [Funktion.prototype.bind] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) – Pointy

+0

@ Failwyn gibt es keine Erwähnung in dieser Frage von jQuery – Pointy

Antwort

0
  1. Kopieren Sie einen Verweis auf das in der findById Funktion.

    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. Verwenden call oder apply.

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

wo immer Sie Funktionen verwenden, tun es auf diese Weise:

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

Probe:

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

Es ist schwer zu verstehen ... wie es bei meinem Code arbeiten wird? Funktion callback.apply (dies, Knoten) { if (parseInt (node.id) === idNumber) returnItems = Knoten; }; ?? –

+0

nein nein nein, es ist nicht auf die Definition. es ist, wenn Sie die Funktion aufrufen. Anstatt Callback (Node) aufzurufen, nennen Sie es: callback.apply (this, node) – Mayday

Verwandte Themen