I am new to JavaScript and knockoutjs. This is probably the most basic of questions and I hope not too stupid a one to ask here…
On a lot of the documentation I see the view models being declared in two different ways. Sometimes they are declared as functions and at other times they are plain objects. What is the difference and what are the scenarios in which I would use each.
var viewModel = {
property: ko.observable()
}
Or
var viewModel = function(){
this.property = ko.observable()
}
Apologies if this is a dumb question….
The second example is a constructor function in JavaScript – since JavaScript doesn’t have “classes”, that’s how you implement “classlike” instance-creation.
More info: http://www.javascriptkit.com/javatutors/oopjs2.shtml