Durandal 计算原型方法
Durandal computed prototype methods
是否可以在我的视图模型原型上定义计算 属性?据我所知,在声明时无法访问正确的 this
值。
我可以将它放在构造函数中,但如您所知,它会针对每个实例重新定义。所以归根结底这只是性能的问题。
我的视图模型的 AMD 模块:
function viewModel(){
// this.someComputed works
}
viewModel.prototype.someComputed = ko.pureComputed(function(){
}/*, no context yet..? */);
viewModel.prototype.activate = function(){
// 'this' is an instance of the view model (I presume)
// should I attach it here?
};
return viewModel;
Is it possible to define a computed property on my view model prototype? As far as I can tell, there is no way to access a proper this
value at the time of declaration.
正确。所以答案是否定的,你不能在原型方法上使用计算。
是否可以在我的视图模型原型上定义计算 属性?据我所知,在声明时无法访问正确的 this
值。
我可以将它放在构造函数中,但如您所知,它会针对每个实例重新定义。所以归根结底这只是性能的问题。
我的视图模型的 AMD 模块:
function viewModel(){
// this.someComputed works
}
viewModel.prototype.someComputed = ko.pureComputed(function(){
}/*, no context yet..? */);
viewModel.prototype.activate = function(){
// 'this' is an instance of the view model (I presume)
// should I attach it here?
};
return viewModel;
Is it possible to define a computed property on my view model prototype? As far as I can tell, there is no way to access a proper
this
value at the time of declaration.
正确。所以答案是否定的,你不能在原型方法上使用计算。