I am following Douglas Crockford's tutorial on the visibility of JavaScript variables and functions here : http://javascript.crockford.com/private.html
I have written the following MyClass.js file and I am running it from the terminal using node. Below I show my output on the terminal and my class. I do not understand why I get returned an "undefined" (instead of just true) and also, why my console.log(log) is not showing anywhere?
$ node MyClass.js
undefined
true
And my class
function MyClass(log)
{
this.log = log;
var that = this;
function _evaluate (log)
{
console.log(log);
return true;
};
this.evaluate = function()
{
return _evaluate() ? true : false;
};
}
var myObject = new MyClass('this is a test');
console.log(myObject.evaluate());
You call _evaluate
with no arguments, so it prints undefined
once (line 7), then true
(line 19).
Finally it prints undefined
because your script as a whole has no return value.