27

If I understand correctly, according to Douglas Crockford http://javascript.crockford.com/private.html, the "privileged" methods are similar to what we know as "public" methods. and "public" methods are something that's a bit different.

Here's how I understand it:

  1. "Privileged" methods can access private variables since it is defined inside the closure when the rest of the private variables were defined.

    var C = function(){
      var private;
      this.privilegedMethod = function(){
           /* blah blah */
      };
    }
    var cObj = new C();
    
  2. "Public" methods are the ones that are added to the object outside of the object itself, through prototype.

    var C = function(){
       /* blah blah */
    }
    C.prototype.publicMethod = function(){
        /* blah blah */
    };
    var cObj = new C();
    

I find these definitions of "privileged" and "public" very confusing. I think the "privileged" method is nothing more than actually a public method as we know it from object oriented programming. And I think the "public" method is the one that should be named as something else. If you think about it, it's a weird type of function, it's a member of the object but it cannot access any other private variables, which means it doesn't contribute to encapsulation. It's almost like an independent helper method for the object.

So I was wondering, why did Douglas Crockford come up with these confusing terms? And why have the javascript community adopted these terminologies? Or if I'm wrong about something, please correct me.

Vlad
  • 8,038
  • 14
  • 60
  • 92
  • 1
    JavaScript is not an OOP language, but rather is based on prototypical inheritance. The differences are worth naming differently :) – singpolyma Sep 15 '12 at 17:32
  • @singpolyma: http://stackoverflow.com/questions/3536048/is-oop-possible-in-javascript – Felix Kling Sep 15 '12 at 17:59
  • The best thing to do imo is to ignore all 'visibility' work arounds and use JavaScript as it is. If you have private properties then document them accordingly. Using closures to simulate visibility only goes so far. It is not a magic bullet and can be huge pain when stuff gets more complex (inheritance for example). – Felix Kling Sep 15 '12 at 18:02
  • @FelixKling sure, I guess it depends how one defines "OOP". – singpolyma Sep 15 '12 at 18:21
  • 3
    @singpolyma As far as I know, prototype is an implementation of object oriented programming, versus classical implementation which is employed by most other OOP languages. Also, I do not think the differences are worth naming differently, since the terms "public" and "private" are basically concepts from encapsulation, which is in turn a concept from object oriented programming. My question is "what makes javascript so different that you have to make those changes while making things so confusing?" – Vlad Sep 15 '12 at 19:21
  • 1
    As far as I can see, "privileged" is just a term that Crockford has invented, that doesn't seem to be referenced anywhere else in the literature? I agree with the author of this question that this is just a "public" method as would be understood in any other OO (or OO-like) context. Crockford's "public" methods are just a particular type of public method that doesn't happen to access any shared context - perhaps because it can't due to the way it's been implemented. I don't see any need to redefine the meaning of the word "public" to cover this and IMO it's unnecessarily confusing. – Dan King Dec 02 '16 at 12:27

5 Answers5

10

Since there are no scope modifiers like public/private/protected in JavaScript, in order to come close to the OOP world, Douglas Crockford is using such names, not to confuse anyone coming from, lets say Java

The privileged method can see variables, defined inside the function (An important note here - in JavaScript, the only scope is function scope. There's no block scope) so they are "privileged". Yes, they can be called from an object instance, but the important thing here is, that they see all the stuff, declared with var (the real private stuff)

On the other hand, public methods, that are attached to the prototype of the object have one more important thing - they are evaluated once and seen for all instances of the given object.

If you use the this keyword inside a prototype method, it will point to the current instance of the Object but you will be able to see only things, that are defined within this.

I don't know if it gets clear, but the main thing here is that JavaScript is prototype based language and the prototype chain was introduced in the language in order to make inheritence possible.

Radoslav Georgiev
  • 1,366
  • 8
  • 15
  • 2
    Thank you for taking time to answer, but this doesn't really answer my question. You are just re-iterating the definition of privileged vs. public given by Crockford. My question was *why* they were named that way. Why not just leave the notion of "public" as is--instead of renaming it "privileged"--and invent a new term for what Crockford calls "public"? – Vlad Sep 15 '12 at 19:13
  • 4
    Well, my best guess is not to confuse people from OOP world with already predefined terms like public and private :) JavaScript is really confusing oO – Radoslav Georgiev Sep 15 '12 at 19:34
  • A privileged method has the privilege of accessing private variables and functions. Public methods do not have this privilege. If, in your constructor method, you create a var like "var x = 1", a privileged method can do "x++" and x will now equal 2. A public function cannot access x; it is undefined in that scope. If you wanted a public function to access x, you have to attach it to the instance: "this.x = 1" – wpjmurray Feb 24 '16 at 17:49
3

Vlad, I agree with you : I am confused too! Look here (from http://javascript.crockford.com/private.html):

function Container(param) {
    // methode privee
    function dec() {
    console.log('private method is looking for private member secret : ' + secret);
        if (secret > 0) {
            secret -= 1;
            return true;
        } else {
            return false;
        }
    }
    // membres privées
    var secret = 3;
    var that = this;
    // méthode privilégiée
    this.service = function () {
    console.log('priviligied method is looking for private member secret : ' + secret);
        return dec() ? that.member : null;
    };
    // membres publiques
    this.member = param;

}
var myContainer = new Container('abc');

Container.prototype.stamp = function (string) {
    console.log('public method is looking for private member secret : ' + this.secret);
    return this.member + string;
}

console.log(myContainer.stamp('def'));
//for (i=0;i<4;i++)
console.log(myContainer.service());

priviliged vs public

This jsfiddle sample will display :

public method is looking for private member secret : undefined
abcdef
priviligied method is looking for private member secret : 3
private method is looking for private member secret : 3
abc

So, the answer : public methods <=> priviliged methods isn't it?

Yoong Kim
  • 310
  • 1
  • 6
  • 13
1

In a traditional OOP language, all of the members of a class have access to all of the other members of a class.

This is not the case in Javascript. A public method may have access to private data that none of the other members of the class (viz. constructor function) know about. Equally, other members of the class may have data that the method cannot see.

Consider below:

function TheirTrait() {
  var privateData = "foo";
  this.privilegedMethod = function() {
    return privateData;
  }
}

function MyClass() {
  var privateData = undefined;
  this.publicMethod = function() {
    return privateData;
  }
  TheirTrait.apply(this, arguments);
}

var myObject = new MyClass();
myObject.privilegedMethod() // returns "foo"
myObject.publicMethod() // returns undefined

As you can see, both publicMethod and privilegedMethod are public in the sense that they can both be accessed externally, but privilegedMethod has access to additional data.

marrs
  • 61
  • 3
0

The key difference between both kind of methods is that Privileged Methods CAN WE INHERIT, this means that child classes can access them directly, but Public Methods are not available for child classes, of course in a classical inheritance approach.

Hope this be useful.

  • that protected method can be inherited ok... that public methods are not available for child classes: *** I disagree ***. It's private methods which are neither accessible to daughter classes and external world. – Stephane Rolland Dec 31 '14 at 18:09
0
  1. Privileged methods are created using "this" keyword and public methods are created using prototype property of the constructor function.

  2. Privileged methods can access private variables and methods. Public methods can call privileged methods but not private methods.

  3. Both privileged and public methods available within and outside the object.

K Vinodh Kumar
  • 171
  • 1
  • 1
  • 6