In C++, the language I\'m most comfortable with, usually one declares an object like this:
class foo
{
public:
int bar;
int getBar() { return bar; }
In javascript this always refers to the owner object of the function. For example, if you define your function foo() in a page, then owner is the javascript object windows; or if you define the foo() on html element , then the owner is the html element body; and likewise if you define the function onclick of element , then the owner is the anchor.
In your case, you are assigning a property bar to the 'owner' object at the begining and trying to return the local variable bar.
Since you never defined any local varialbe bar, it is giving you as bar is undefined.
Ideally your code should have defined the variable as var bar; if you want to return the value zero.