computed observables are evaluated immediately upon creation. In your case, viewModel has not yet been created, so this causes an error.
A couple of alternatives:
-Create it outside your source object literal:
var viewModel = { firstName: ko.observable("r"), lastName: ko.observable("j"), }; viewModel.fullName = ko.computed(function() { return viewModel.firstName(); });
-Create your view model in function:
var ViewModel = function() { this.firstName = ko.observable("r"); this.lastName = ko.observable("j"); this.fullName = ko.computed(function() { return this.firstName(); }, this); }; ko.applyBindings(new ViewModel());
source share