0

I am trying to understand how private and protected properties work in javascript.

I have gotten the general idea, and was playing around making test call between properties.

So far, I have found that:

  • Public can call protected
  • Protected can call private
  • Public can t call private
  • Private can call Public
  • Protected can call Public

So far so good, but when I tried to make a private function call a protected one, I get a undefined error.

test.js:

var Test = require('./test_.js'),
    test = new Test();

test.prot_func();

test_.js:

function publ_func() {
    console.log('Public');
}

function Test() {
    var priv_var = 0;

    this.prot_func = function prot_func() {
        //OK
        console.log('Protected');
        priv_func();
    };

    function priv_func() {
        //OK
        console.log('Private');
        //OK
        //publ_func();
        test_prot();
        //test_prot is not defined?
        //Also tried: this.test_prot();
    }

    this.test_prot = function test_prot() {
        console.log('Protected');
        publ_func();
    };
}

Test.prototype.publ_func = publ_func;

module.exports = Test;

Do I just have a syntax error, or is there no way to call a protected method from a private one?

DrakaSAN
  • 6,955
  • 7
  • 43
  • 85
  • 2
    The answer to this is easy, there are no private and protected properties in javascript, just scope, so you don't have to worry about it. – adeneo Dec 07 '15 at 16:21
  • Your issue here all revolves around the call to `priv_func()` - the way it's being called, there is no explicit object being defined for `this` within the function. As @adeneo says, you shouldn't really learn about private/protected/public in JS - much better to learn about scope and how to control `this`. – James Thorpe Dec 07 '15 at 16:24
  • @adeneo Douglas Crockford maydisagree with you: http://javascript.crockford.com/private.html. Also, my poblem is how to call `test_prot()` from `priv_func()` – DrakaSAN Dec 07 '15 at 16:24
  • 1
    Pffft, Douglas Crockford my a$$, he knows nothing. – adeneo Dec 07 '15 at 16:25
  • These are just coding conventions, not really reinforced by the language. – MinusFour Dec 07 '15 at 16:25
  • @JamesThorpe: I ll come back after reading more about closure/scope. But isn t that interpretation of priv/prot/publ function use the scope to simulate the classic OOP approach? – DrakaSAN Dec 07 '15 at 16:31
  • @DrakaSAN You can simulate private and public member functions of an object within JavaScript, but it works a little differently than you'd expect. In JavaScript *everything* is an object, so it's all about scope. – Nick Zuber Dec 07 '15 at 16:32
  • @DrakaSAN, i think it would be extremely healthy if you don't try to force JS into classical OOP behavior. – MinusFour Dec 07 '15 at 16:34
  • 2
    Yes, you can _simulate_ it, but to so do, you need a knowledge of how the scoping works, and how to control `this` properly. [This question](http://stackoverflow.com/questions/3127429/how-does-the-this-keyword-work) has a good summary of `this`. Once you have a good understanding of these topics, simulating pub/priv/prot properly is certainly doable. – James Thorpe Dec 07 '15 at 16:34
  • Of course you can "simulate" private members, all you have to do is define something out of scope, and it's "private", but it's really all about what scope it's in, there are no `private` or `protected` keywords that magically make variables this or the other (but it's coming in a later version) – adeneo Dec 07 '15 at 16:34
  • @adeneo: Nice to know :) If I may diverge from the original topic, which author would you reccommend for JS related topic, Crockford excluded? – DrakaSAN Dec 07 '15 at 16:37
  • @JamesThorpe: Thanks you for your time and this useful link :) – DrakaSAN Dec 07 '15 at 16:37
  • Crockford isn't bad per-se, but don't take everything he says as gospel. – James Thorpe Dec 07 '15 at 16:37
  • ^ That, Crockford is actually great, the guy invented JSON for gods sake. But, a lot of what he says is outdated, or just plain wrong. He's the kind of guy that either says genious things, or it's just plain stupidness, and it's hard to tell which one it is sometimes. The book to read, cover to cover, is as usual [Knuth's Art of Computer Programming](http://www.amazon.com/Computer-Programming-Volumes-1-4A-Boxed/dp/0321751043) – adeneo Dec 07 '15 at 16:42
  • For a good example of Crockford vs Not-Crockford, see JSLint (Crockford "_The_ javascript...") and JSHint (Not Crockford "_A_ javascript...") – James Thorpe Dec 07 '15 at 16:44

2 Answers2

1

To understand why this happens, first you need to understand each part of your object.

The function test_prot is a property of your Test object. Same for prot_func.

However, priv_func is a regular function that is simply defined within the scope of your Test object. Basically, that function isn't really related to your object.

This is why when you call test_prot it is undefined because it is a property of Test and you're not giving it a calling object.

function priv_func() {
    test_prot(); // <--- there is no calling object, even though this is a property of Test
};

So if you want to call test_prot within that function, you need to give it a calling object. Something like this would work:

...
// Define a reference to Test object within scope of Test
var self = this;
...

function priv_func() {
    self.test_prot(); // <--- now it has a calling Test object, and will work
};
Nick Zuber
  • 4,769
  • 2
  • 19
  • 45
0

try:

function Test() {
    var priv_var = 0;
    vat that = this;

    this.prot_func = function prot_func() {
        //OK
        console.log('Protected');
        priv_func();
    };

    function priv_func() {
        //OK
        console.log('Private');
        //OK
        //publ_func();
        that.test_prot();
        //test_prot is not defined?
        //Also tried: this.test_prot();
    }

    this.test_prot = function test_prot() {
        console.log('Protected');
        publ_func();
    };
}

and read about closures in js!

Pedro Ramon
  • 124
  • 1
  • 4