In knockout js I see View Models declared as either:
var viewModel = {
firstname: ko.observable(\"Bob\")
};
ko.applyBindings(viewModel );
I use a different method, though similar:
var viewModel = (function () {
var obj = {};
obj.myVariable = ko.observable();
obj.myComputed = ko.computed(function () { return "hello" + obj.myVariable() });
ko.applyBindings(obj);
return obj;
})();
Couple of reasons:
this
, which can confusion when used within ko.computed
s etcnew viewModel()
)