2013-06-04 36 views
0

我从它的官方网站学习淘汰赛,这里是我从网站采取的教程为什么我们通过“这”在ko.computed

// This is a simple *viewmodel* - JavaScript that defines the data and behavior of your UI 
function AppViewModel() { 
this.firstName = ko.observable(''); 
this.lastName = ko.observable("Bertington"); 
this.fullName = ko.computed(function() { 
return this.firstName() + " " + this.lastName();  
}, this); 
} 

// Activates knockout.js 
ko.applyBindings(new AppViewModel()); 

我要问,什么是这个传递的目的作为参数来计算功能

ko.computed(function() { 
return this.firstName() + " " + this.lastName();  
}, this); 

感谢

+3

您是否阅读过**在http://knockoutjs.com/documentation/computedObservables.html管理“this”**部分?有些事情还不清楚? – nemesv

回答

2

因为在Javascript 是大多数时候不是你希望它是从C#,C +什么+或Java开发人员POV。

这个参数确保这个实际上绑定到视图模型,当计算的observable的新值被计算时,而不是调用方法的这个上下文,例如事件处理程序。

+0

非常感谢您的清除 – Ancient

+2

您也可以通过添加一个“var self = this”作为ViewModel函数的第一行来绕过它。然后,在你通常所称的“this”的任何地方,你都会提到“self”,包括在计算的函数内部。 –

相关问题