3

I have code as below.

var Main = function () {
    var a, b, c, d;
    a = 1;
    b = true;
    c = undefined;

    var _private = function () {
        return 'Function with Private acceess';
    };

    this.getPublic = function () {
        return 'Function with Public access';
    };

    this.getPrivate = function () {
        _private();
    };

};

var o = new Main();
console.log(o.getPublic());
console.log(o.getPrivate());

In the code above I am trying to access the private method of the Main object o through the public method getPrivate(). But in the console the result is

undefined

Why is the _private not returning the desired value?

Mozak
  • 2,738
  • 4
  • 30
  • 49
  • 3
    `_private()` does its best to return a value, but `getPrivate()` does not relay it to its own caller. – Frédéric Hamidi Aug 16 '13 at 12:13
  • Isn't this in the class of typo questions? – Esailija Aug 16 '13 at 12:13
  • @Esailija, not sure. Still thinking, though. – Frédéric Hamidi Aug 16 '13 at 12:13
  • It could be that OP has too minimal understanding or has understanding but made a typo-like mistake. :P – Esailija Aug 16 '13 at 12:13
  • 1
    @Esailija, indeed, but the right angle to consider is if this question can benefit to further readers. Forgetting a `return` statement in the call chain is not a big deal in other languages (resulting in a warning at least), but not so in Javascript. There is a quick, to-the-point, upvoted answer. Keeping this question open looks like the better choice here. – Frédéric Hamidi Aug 16 '13 at 12:18
  • In Java this would be a compile time error, in C, C++ a compile-time warning, in Perl the value of the last expression would be returned... – Antti Haapala -- Слава Україні Aug 16 '13 at 12:32
  • Thanks Esailija and Hamidi for the useful comment. – Mozak Aug 16 '13 at 12:32
  • Just one more clarification here, in the scope of getPrivate, the _private is a closure(this is what I can clearly see from the scope variables of chrome dev tools), so I can't call a closure like this and instead I need to return it so that it can execute it's statements. Does this makes any sense? – Mozak Aug 16 '13 at 12:36
  • 3
    You should maybe look into how constructor functions are usually applied. Currently it looks like you're not using prototype at all and may as well use a function returning an object literal (if you want to use private value properties) or properly implement constructor functions: http://stackoverflow.com/questions/16063394/prototypical-inheritance-writing-up/16063711#16063711 – HMR Aug 16 '13 at 12:42

4 Answers4

7

You forgot the return statement. Try the following:

this.getPrivate = function () {
    return _private();
};

If no value is explicitly returned from a Javascript function, the function is considered to return undefined; no warning will be emitted.

2

You forgot to "return" the value.

this.getPrivate = function () {
    return _private();
};
samuelgrigolato
  • 192
  • 1
  • 6
2

Oops..you forgot to return value...try this

var Main = function () {
    var a, b, c, d;
    a = 1;
    b = true;
    c = undefined;

    var _private = function () {
        return 'Function with Private acceess';
    };

    this.getPublic = function () {
        return 'Function with Public access';
    };

    this.getPrivate = function () {
        return _private();
    };

};

var o = new Main();
console.log(o.getPublic());
console.log(o.getPrivate());
Amith
  • 1,424
  • 1
  • 10
  • 22
0

change like this return _private();

Man Programmer
  • 5,300
  • 2
  • 21
  • 21