Skip to main content
Notice removed Reward existing answer by nemesv
Bounty Ended with RP Niemeyer's answer chosen by nemesv
Notice added Reward existing answer by nemesv
Bounty Started worth 50 reputation by nemesv
edited title
Link
Kev
  • 120.1k
  • 53
  • 308
  • 396

What's the difference Difference between knockout View Models declared as varobject literals vs functions?

added 2 characters in body
Source Link
Thomas Eding
  • 36.9k
  • 14
  • 84
  • 112

In knockout js I see View Models declared as either:

var viewModel = { firstname: ko.observable("Bob") }; ko.applyBindings(viewModel ); 

or:

var viewModel = function() { this.firstname= ko.observable("Bob"); }; ko.applyBindings(new viewModel ()); 

What's the difference between the two, if any?

I did find this discussion on the knockoutjs google group but it didn't really give me a satisfactory answer.

I can see a reason if I wanted to initialise the model with some data, for example:

var viewModel = function(person) { this.firstname= ko.observable(person.firstname); }; var person = ... ; ko.applyBindings(new viewModel(person)); 

But if I'm not doing that does it matter which style I choose?

In knockout js I see View Models declared as either:

var viewModel { firstname: ko.observable("Bob") }; ko.applyBindings(viewModel ); 

or:

var viewModel = function() { this.firstname= ko.observable("Bob"); }; ko.applyBindings(new viewModel ()); 

What's the difference between the two, if any?

I did find this discussion on the knockoutjs google group but it didn't really give me a satisfactory answer.

I can see a reason if I wanted to initialise the model with some data, for example:

var viewModel = function(person) { this.firstname= ko.observable(person.firstname); }; var person = ... ; ko.applyBindings(new viewModel(person)); 

But if I'm not doing that does it matter which style I choose?

In knockout js I see View Models declared as either:

var viewModel = { firstname: ko.observable("Bob") }; ko.applyBindings(viewModel ); 

or:

var viewModel = function() { this.firstname= ko.observable("Bob"); }; ko.applyBindings(new viewModel ()); 

What's the difference between the two, if any?

I did find this discussion on the knockoutjs google group but it didn't really give me a satisfactory answer.

I can see a reason if I wanted to initialise the model with some data, for example:

var viewModel = function(person) { this.firstname= ko.observable(person.firstname); }; var person = ... ; ko.applyBindings(new viewModel(person)); 

But if I'm not doing that does it matter which style I choose?

Source Link
Kev
  • 120.1k
  • 53
  • 308
  • 396

What's the difference between knockout View Models declared as var vs functions?

In knockout js I see View Models declared as either:

var viewModel { firstname: ko.observable("Bob") }; ko.applyBindings(viewModel ); 

or:

var viewModel = function() { this.firstname= ko.observable("Bob"); }; ko.applyBindings(new viewModel ()); 

What's the difference between the two, if any?

I did find this discussion on the knockoutjs google group but it didn't really give me a satisfactory answer.

I can see a reason if I wanted to initialise the model with some data, for example:

var viewModel = function(person) { this.firstname= ko.observable(person.firstname); }; var person = ... ; ko.applyBindings(new viewModel(person)); 

But if I'm not doing that does it matter which style I choose?