0

Newbie Javascript question:

how does one access a private class property in a public method? In all the example I see a public prototype function accessing public (this.property). Is it possible to access a private property in a public method?

reza
  • 5,972
  • 15
  • 84
  • 126
  • 1
    I don't believe JavaScript has a notion of private or public properties. In fact, classes aren't native to the language. One usually has to use 3rd party libraries to get OOP features. – Farley Knight Nov 27 '13 at 01:11
  • @FarleyKnight JavaScript uses prototype based OO, even 3rd party libraries won't change that. If you use TypeScript or Dart you use a language that is class based and compiles to JavaScript. You'll find that these libraries compile private variables to be public since even with prototype based you can't have private variables. You can completely hide members by using closures but that's not really private (instances of same type cannot see "privates" of other instances). – HMR Nov 27 '13 at 02:46
  • I've written an introduction to JavaScript's prototype and constructor functions. The way JS OOP works isn't class based and may take some getting used to. Before going into more complicated patterns that could simulate privateness I'd advice you to read and understand the basics so you'll know what you are sacrificing by using these patterns: http://stackoverflow.com/a/16063711/1641941 – HMR Nov 27 '13 at 02:49
  • @HMR great advice and great write up. Thank you so very much – reza Nov 27 '13 at 16:17

2 Answers2

3

This pattern is known as a "privileged" method. It looks something like this:

function MyClass() {
  var secret = "foo";
  this.tellSecret = function() {
    return secret;
  };
}

var inst = new MyClass();
console.log(inst.tellSecret()); // => "foo"
console.log(inst.secret);       // => undefined

This works because the private variable is in a closure. The problem with this is that we are putting the privileged method on each instance, rather than the prototype. This is not ideal. Often, instead of having private variables in JavaScript, authors will just use a leading underscore which is conventionally used to imply that public methods/properties should be treated as private:

function MyClass() {
  this._secret = "foo";
}

MyClass.prototype.tellSecret = function() {
  return this._secret;
};
sbking
  • 7,630
  • 24
  • 33
0

Here is a little demo:

var Foo = function(name){ 
    this.name = name; 
    var t = name; 
    if(typeof(this.show) != 'function'){
        Foo.prototype.show = function(){
            console.log(t);
            console.log(this.name);
        };
    }
};

var a = new Foo('a');
var b = new Foo('b');
b.show(); // ...

Hope it can help you out.

Teddy
  • 787
  • 5
  • 13
  • This does not do what you think it does. [see this](http://jsfiddle.net/aAnM4/). Displays "a" when you expect "b". – Joe Enzminger Nov 27 '13 at 02:27
  • You may think that `t` is specific to each instance but it really is not. A closure is created once when the first instance is created and all other instances will share it. – HMR Nov 27 '13 at 02:58