简体   繁体   中英

context in class method as variable

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))

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 {
    //...
}

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM