The first method defines the constructor of the object, but does not instantiate a new object, you can pass arguments in this way. If you create multiple objects / models, this will definitely be less cumbersome than the second method.
The second way is to use the object initializer syntax, which creates a new object in memory with any fields in which you set it. In general, this results in a smaller code size; if you create two objects of the same or similar structures, use the first method.
, . , .
:
function AppViewModel(fName, lName) {
var self = this;
self.firstName = ko.observable(fName);
self.lastName = ko.observable(lName);
}
...
var appViewModel = new AppViewModel("Bert", "Bertington");
, :
var appViewModel = {
this.firstName = ko.observable("Bert"),
this.lastName = ko.observable("Bertington")
};
AppViewModel, . new AppViewModel("Joe", "Shmoe")