I have been using KnockoutJS to make the UI for my Expense Tracking application more dynamic and have been LOVING it. When I began learning KnockoutJS, I had a hard time trying to get the hang of observables. Now if you’ve never heard of KnockoutJS, then you may not know what an observable is. So, what are observables? They are model properties that can automatically detect dependencies and be notified of changes. In other words, when a property that has been declared to be a Knockout observable detects changes, the property changes accordingly. They’re pretty awesome. They help cut down significantly on writing code.
Now to the main point of this post: when to use an observable with parentheses? Sometimes when using observables, they can be written as:
self.observableValue(); self.observableValue; So what’s the difference? The difference is that observable properties that are bound with parentheses can only be read. They will return a value. For example:
self.playerOneWeapon = ko.observable("Axe"); console.log(self.playerOneWeapon()); If you were to call the property without the parentheses, you would not see the value, “Axe”, but instead a function similar to below:
function c(){if(0<arguments.length)return c.Ka(d,arguments[0])&&(c.P(),d=arguments[0],c.O()),this;a.k.zb(c);return d}
So always keep in mind, if you want to read the data of an observable property, name it as a function (bind it with parentheses).