KnockoutJS Discovery Log - Observables - When to use an observable with parentheses?

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:

  1. self.observableValue(); // with parentheses
  2. // or
  3. self.observableValue; // without parentheses

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:

  1. // Observable property with a value set
  2. self.playerOneWeapon = ko.observable("Axe");
  3. // Displaying the value in the console
  4. console.log(self.playerOneWeapon()); // Console should show "Axe" as the value, without the double quotes of course.

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).