刚刚开始使用淘汰赛和 javascript。我一直在设置对象 monthData 的 mPrice 值。
function monthData(mYear, mMonth, mSS, mMs, mLimit, mPerItem, mStartingPrice) {
var self = this;
self.mYear= mYear;
self.mMonth = mMonth;
self.mSs = mSS;
self.mMs = mMs;
self.mTotal = mSS + mMs;
self.mLimit = mLimit;
self.mPerItem = mPerItem;
self.mStartingPrice = mStartingPrice;
};
这是它停止工作。如果我设置 self.mPrice = 0 我至少打印出所有月份。
self.mPrice = ko.computed(function() {
var limit = self.mLimit;
var perItem = self.mPerItem;
var startingPrice = self.mStartingPrice;
var total = self.mTotal;
if (total <= limit) {
return startingPrice;
} else {
var aboveLimit = total - limit;
var extra = aboveLimit * perItem;
return startingPrice + extra;
}
});
}
function statViewModel() {
this.tak = ko.observable(100);
this.styckpris = ko.observable(10);
this.grundpris = ko.observable(500);
var self = this;
// testing with some months
self.allMonths = ko.observableArray([
new monthData(2013, 1, 412, 142, this.tak, this.styckpris, this.grundpris),
new monthData(2013, 1, 412, 142, this.tak, this.styckpris, this.grundpris),
new monthData(2013, 1, 412, 142, this.tak, this.styckpris, this.grundpris),
new monthData(2013, 1, 412, 142, this.tak, this.styckpris, this.grundpris),
new monthData(2013, 1, 412, 142, this.tak, this.styckpris, this.grundpris)
]);
}
-编辑:就在我发布之后,我发现了一个失踪的paranteses。现在我把它打印出来,但它说:
function d(){if(0<arguments.length){if(!d.equalityComparer||!d.equalityComparer(c,arguments[0]))d.H(),c=arguments[0],d.G();return this}b.r.Va(d);return c}NaN
html:
<p>Tak: <input data-bind="value: tak" /></p>
<p>Styckpris: <input data-bind="value: styckpris" /></p>
<p>Grundpris: <input data-bind="value: grundpris" /></p>
<table>
<thead>
<tr>
<th>År</th><th>Månad</th><th>SS</th><th>MS</th><th>Total</th><th>Pris</th>
</tr>
</thead>
<tbody data-bind="foreach: allMonths">
<tr>
<td><span data-bind="text: mYear"></span></td>
<td><span data-bind="text: mMonth"></span></td>
<td><span data-bind="text: mSs"></span></td>
<td><span data-bind="text: mMs"></span></td>
<td><span data-bind="text: mTotal"></span></td>
<td><span data-bind="text: mPrice"></span></td>
</tr>
</tbody>
</table>