Knockout JS Model Inheritance

前端 未结 5 654
一生所求
一生所求 2020-12-23 17:14

I have three relatively similar knockout models in my application and I would like to extend a base model to combine common properties rather than repeat myself three times.

5条回答
  •  不思量自难忘°
    2020-12-23 18:11

    I've done something similar, with a lot of trial and error, but I got this to work for me:

    var StandardItemModel = function (item, cartItemTypes) {
        var self = this;
        ItemModel.call(self, item)
    }
    

    You then need to add a prototyped constructor:

    StandardModel.prototype = new ItemModel();
    

    If you want to have common methods, then you need to add them to the base classes using prototype to add them, then call them in the higher class using:

    ItemModel.prototype.methodName.call(self, parameters);
    

提交回复
热议问题