Both code blocks below work, in context and seem completely functionally equivalent. I understand js prototypes reasonably well, so I'm not asking about them per se (unless that is the only difference).
Rather, comparing two simple ways to put a method on a view model as shown below, are there any implications / differences for Knockout, e.g. binding time?
define(["knockout", "text!./home.html"], function(ko, homeTemplate) { // <-- An AMD Module
function HomeViewModel(route) { var self = this; self.message = ko.observable('Snacks!'); self.eatSomething = function () { self.message('Yum, a viewmodel snack.'); }; } return { viewModel: HomeViewModel, template: homeTemplate }; });
versus adding method via prototype:
define(["knockout", "text!./home.html"], function(ko, homeTemplate) {
function HomeViewModel(route) { this.message = ko.observable('Snacks!'); }; HomeViewModel.prototype.eatSomething = function () { this.message('Yum, the same viewmodel snack, only different?'); }; return { viewModel: HomeViewModel, template: homeTemplate };
});
(The code is a simple mod of Yeoman's scaffolding output via a Knockout generator. It created the boiler plate code for a knockout component, a fairly recent (KO 3.2) and very welcome feature. A nice KO component explainer is here.)