0

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());
FranXh
  • 4,481
  • 20
  • 59
  • 78
  • 2
    You get `undefined` because you are not passing an argument to `_evaluate`, hence `log` is `undefined` inside the function. Not sure what the expected outcome is... just remove the parameter? – Felix Kling Jun 13 '14 at 15:39
  • 1
    Why do you have a parameter for the `_evaluate` function? Since it has the same name as the constructor's parameter (`log`), it's shadowed...and since you call `_evaluate()` (no arguments), it has an `undefined` value. Try doing `console.log(that.log);` (without a `log` parameter for the `_evaluate` function) – Ian Jun 13 '14 at 15:39

1 Answers1

2

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.

Halcyon
  • 57,230
  • 10
  • 89
  • 128