Hi guys!
Could anyone explain me pros and cons of using KO View Model as literal object and as constructor function?
Where is the difference? In which cases I should use one or another variant?
I explain what I mean.
/**
*First variant: View Model as literal object:
*/
ViewModel = {
firstName: ko.observable('John'),
lastName: ko.observable('Smith'),
};
ViewModel.fullName = ko.dependingObservable(function () {
// Some code here
}, ViewModel);
ko.applyBindings(ViewModel);
/**
* Second variant: View Model as constructor function
*/
function ViewModel() {
this.firstName = ko.observable('John'),
this.lastName = ko.observable('Smith'),
this.fullName = ko.dependingObservable(function () {
// Some code here
}, this);
};
ko.applyBindings(new ViewModel());
Thanks!