JavaScript provides prototype inheritance. Objects inherit from other objects. This may be good for method inheritance, but it does not work for property inheritance.
Usually for method inheritance you can use:
function BaseViewModel() { var self = this; self.name = ko.observable(); } function Person() { var self = this; self.firstname = ko.observable(); } Person.prototype = new BaseViewModel();
But it makes all people share the same object as the prototype! When you change the name for one person, the meaning applies to all Persons. I usually use the jQuery expand method.
function Person() { var self = this; $.extend(self, new BaseViewModel()); self.firstname = ko.observable();
}
Thus, the values ββfrom BaseViewModel will be copied to Person.
source share