为什么我得到一个Uncaught TypeError: this.createRow is not a function
关于matrixLengthsAvailable数组构造?

createRow函数在我的视图模型的末尾声明。

function TabBuyHarvesterModel() {
    self = this;

    this.selectedEmote = ko.observable('kappa');
    this.matrixLengthsAvailable = ko.observableArray([
        { length: 10, pctDetails: this.createRow(10) /*ko.mapping.fromJS({ rowLength: 10 })*/ }
        //30,
        //60,
        //180,
        //360,
        //720,
            //1440
        ]);

    this.selectEmote = function (emoteClicked) {
        self.selectedEmote(emoteClicked.emote);
    };

    this.createRow = function (rowLength) {
        var ret = new TabBuyHarvesterMatrixRowModel();
        ret.rowLength(rowLength);
        return ret;
    };
}

最佳答案

正如已经指出的那样,您应该使用self,并在任何地方都正确使用self,然后您需要切换方法的顺序,以便在需要createRow之前对其进行定义。

此处:http://jsfiddle.net/oshmn46o/

function TabBuyHarvesterModel() {
var self = this;

self.createRow = function (rowLength) {
    var ret = new TabBuyHarvesterMatrixRowModel();
    ret.rowLength(rowLength);
    return ret;
};

self.selectedEmote = ko.observable('kappa');
self.matrixLengthsAvailable = ko.observableArray([
    { length: 10, pctDetails: self.createRow(10) /*ko.mapping.fromJS({ rowLength: 10 })*/ }
    //30,
    //60,
    //180,
    //360,
    //720,
        //1440
    ]);

self.selectEmote = function (emoteClicked) {
    self.selectedEmote(emoteClicked.emote);
};


}

08-16 06:37