¿Se puede acceder a una propiedad de clase privada en un método público?

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?

preguntado el 27 de noviembre de 13 a las 01:11

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. -

@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). -

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: stackoverflow.com/a/16063711/1641941 -

@HMR great advice and great write up. Thank you so very much -

2 Respuestas

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;

respondido 27 nov., 13:01

Aquí hay una pequeña demostración:

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

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

Espero que pueda ayudarte.

respondido 27 nov., 13:02

Esto no hace lo que crees que hace. mira esto. Displays "a" when you expect "b". - joe enzminger

Puedes pensar que 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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.