0

There is piece of code:

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

How to import context in to iterator and callback function? if I put console.log(this) into function iterator() - this will be 'window', but not my object. Also it shouldn't be this.callback this.iterator etc. As I understand it should be like call/apply or bind. How do it?

Triple -B
  • 251
  • 2
  • 11
  • I think $.proxy is what you are looking for http://stackoverflow.com/questions/4986329/understanding-proxy-in-jquery – Failwyn Apr 20 '16 at 14:19
  • 1
    [Function.prototype.call](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) [Function.prototype.apply](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply) [Function.prototype.bind](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind) – Pointy Apr 20 '16 at 14:19
  • @Failwyn there's no mention made in this question of jQuery – Pointy Apr 20 '16 at 14:20

2 Answers2

0

wherever you use functions do it this way:

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

Sample:

function callable() {
   console.log(this);
}
callable(); //logs window
callable.apply({}); //logs {}
Mayday
  • 3,482
  • 2
  • 15
  • 44
  • It is hard to understand...how it will be work at my code? function callback.apply(this,node) { if (parseInt(node.id) === idNumber) returnItems = node; };?? – Triple -B Apr 20 '16 at 14:48
  • no no no, its not on the definition. its when you call the function. Instead of calling callback(node) you call it: callback.apply(this,node) – Mayday Apr 20 '16 at 15:21
0
  1. Copy a reference to this inside the findById function.

    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. Use call or apply.

    for (var i = data.length - 1; i >= 0; i--) {
      iterator.call(this, data[i], callback);
    };
    
pishpish
  • 2,359
  • 11
  • 21