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?
Ich denke .proxy $ ist das, was Sie suchen http://stackoverflow.com/questions/4986329/understanding-proxy-in-jquery – Failwyn
[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
@ Failwyn gibt es keine Erwähnung in dieser Frage von jQuery – Pointy