1

There is a class as example:

var Class = {
   initialize: function(data){
      this.data = data;
   },
   add: function(node){
      //add object
   },
   remove: function(node){
      //remove object
   },
   findByIndex: function(id){

   },
}

and so on.

Question: How to import context into findByIndex?

For example I need to use findByIndex in another class as function. I understand that it should be bind, but how to use it in my class?

findByIndex:function(id, ???context???){
      ????
}

///////////////////////ADD////////////////// this is method

L.Bookmarks = L.Class.extend({
    findByIdRecursive: function(idNumber) {
        var data = this.childNodes;
        var returnItems = {};

        function callback(node) {
            if (parseInt(node.id) === idNumber)
                returnItems = node;
        };
        for (var i = data.length - 1; i >= 0; i--) {
            this.iterator(data[i], callback, this);
        };

        return returnItems;
    },

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

this is another class that use method iterator:

L.Book = L.Class.extend({
    findByIdRecursive: function(idNumber) {
        var data = this.childNodes;
        var returnItems = {};

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

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

        return returnItems;
    },
});

As you can see, second class don't have method iterator. But finaly it should use iterator, like owner(with context(this - L.Book))

Triple -B
  • 261
  • 2
  • 12
  • What is `this data`? – Bergi Apr 21 '16 at 17:00
  • What do you mean by "importing context"? What do you want it refer to? What is the body of your method? Please show us how you are going to "*use `findByIndex` in another class as a function*". – Bergi Apr 21 '16 at 17:03
  • Possible duplicate of [How to change the context of a function in javascript](http://stackoverflow.com/questions/1536164/how-to-change-the-context-of-a-function-in-javascript) – Whymarrh Apr 21 '16 at 17:06
  • Possible duplicate of [Can I change the context of javascript “this”?](http://stackoverflow.com/questions/1051237/can-i-change-the-context-of-javascript-this) – Whymarrh Apr 21 '16 at 17:07
  • to Bergi, data - is json object, importing context - mean that if I put into findByIndex console.log(this), this will be class, that call this method. – Triple -B Apr 22 '16 at 08:33

2 Answers2

0

You can use call:

var b = new OtherClass();

// find Index 1 in b (OtherClass)
var element = Class.findByIndex.call(b,1);

See more examples at MDN

Also, if you use ES6 classes (see MDN), you can define a new class using extends so you will inherit the methods from the earlier class:

class NewClass extends Class {
    //...
}
Javier Conde
  • 2,553
  • 17
  • 25
0

with the .bind function you can re-use the findByIndex multiple times in this way

var Utils = {
   findByIndex: function(id){
      return this.data[id];
   }
}

var FirstObject = {
  data : {
    a : 'aaa',
    b : 'bbb'
  }
}

var SecondObject = {
  data : {
    d : 'ddd',
    e : 'eee'
  } 
}


console.log(Utils.findByIndex.call(SecondObject, 'd'))
console.log(Utils.findByIndex.call(FirstObject, 'a'))
maxgallo
  • 458
  • 3
  • 14