Leonardo Wildt
Leonardo Wildt

Reputation: 2599

Knockout Js Observables

I am trying to find definitively when you would use a ("") clear observable over just () and what the difference is. For example

self.Name = ko.observable();

over

self.Name = ko.observable("");

Thank you

Upvotes: 0

Views: 56

Answers (2)

Mateen Kajabadi
Mateen Kajabadi

Reputation: 3634

To add to Gosha_Fighten's answer :

Once you write:

 self.Name = ko.observable();

you are defining an observable variable without initializing it.

Once you write :

self.Name = ko.observable("");

you are defining an observable variable and initializing it with an empty string.

later on if you use it like self.Name() it will return the value of it (if it is already initialized it will return that value, if not will return undefined) Also if you use self.Name("") it will set that observable variable to be an empty string.

So the usage is up to you when you define it.

Upvotes: 2

Gosha_Fighten
Gosha_Fighten

Reputation: 3858

self.Name = ko.observable(); will return undefined if you call alert(self.Name()) and self.Name = ko.observable(""); will return an empty string.

Upvotes: 2

Related Questions