我在Knockout.js中比较两种使用计算的可观察函数的方法
问题:
为什么版本2即使没有将上下文值放在最后也可以工作?
///Version 1
my.Product = function () {
this.id = ko.observable();
this.salePrice = ko.observable();
this.photo = ko.observable();
this.shortDescription = ko.observable();
this.photoUrl = ko.computed
(function () {
return photoPath + this.photo();
},this); //**context**
};
////version 2
my.Product = function () {
var self = this;
self.id = ko.observable();
self.salePrice = ko.observable();
self.photo = ko.observable();
self.shortDescription = ko.observable();
self.photoUrl = ko.computed(function () {
return photoPath + self.photo();
});//why there is no "self" here
};
答案 0 :(得分:2)