1

What JavaScript pattern do I use that gives me:

  1. Public methods on the prototype
  2. Private methods that can be called from #1
  3. Private variables

Other answers I have found seem to return undefined when trying to call a private method from the prototype public method.

Sebastian Patten
  • 7,157
  • 4
  • 45
  • 51
  • Please define "private" and why tell us why you want to use the prototype. – Bergi Oct 31 '15 at 23:16
  • Private being methods not living on the prototype. And prototype for being able to modify class behavior at some future date with a derived sub "class". – Sebastian Patten Oct 31 '15 at 23:28
  • 3
    That's not "private", that's what we called "instance-specific" or "own properties" (and they're still public). – Bergi Oct 31 '15 at 23:31
  • Modifying class behaviour later (from outside) basically *requires* non-private variables – Bergi Oct 31 '15 at 23:32
  • 3
    JavaScript does not have a notion of private methods or properties. You can define variables and functions inside of the constructor, and define methods in the constructor as well, thereby giving the "public" methods access to the "private" variables and functions. None of this, however, involves the prototype. Methods defined on the prototype are not in scope to access the variables and functions defined inside the constructor. So what you want is not possible. Also note that the more you do in the constructor the heavier the processing for instantiation will be. – JAAulde Oct 31 '15 at 23:34
  • True @Bergi I'd forgotten that it was instance specific – Sebastian Patten Nov 01 '15 at 02:17

1 Answers1

0

In fact, you cannot access something private from the prototype.

The prototype works by binding an object as a context to all the methods, so you can use this keyword to access it.

In Object-Oriented Programming, what you call private is something resolved at compile-time, to hint you that the data you are trying to access is not supposed to be readable from outside the class. But at run-time this data will be stored the same way as the other properties.

To have methods accessing private fields, you can rather create method on instance directly rather than on prototype to permit it to access private scope. This is called a privileged method. Check out this article from Douglas Crockford.

var ClassExample = function () {

    var privateProperty = 42
    this.publicProperty = 'Hello'

    var privateMethod = function () {
        return privateProperty
    }

    this.privilegedMethod = function () {
        return privateProperty
    }
}

ClassExample.prototype.publicMethod = function() {
    return this.publicProperty
}

Languages like Typescript, that add classes and typing + privacy settings store private fields alongside public fields.

class ClassExample {

    private privateProperty
    public publicProperty

    constructor () {
        this.privateProperty = 42
        this.publicProperty = 'Hello'
    }

    method () {
        return this.privateProperty
    }

}

will compile in

var ClassExample = (function () {
    function ClassExample() {
        this.privateProperty = 42;
        this.publicProperty = 'Hello';
    }
    ClassExample.prototype.method = function () {
        return this.privateProperty;
    };
    return ClassExample;
})();
kube
  • 13,176
  • 9
  • 34
  • 38