0

I'm not being able to access private variables (or arguments) from a prototyped functions. Take this code:

function My_controller(url) {
  this.list = [1, 2, 3];
}

My_controller.prototype.order_list = function() {
  console.log("url: ", url);
  this.list.push(this.list.splice(0, 1)[0]);
  console.log(this.list);
}

var ct = new My_controller("http://");
ct.order_list();

It works if the function is defined inside de object:

function My_controller(url) {
  this.list = [1, 2, 3];

  this.order_list = function() {
    console.log("url: ", url);
    this.list.push(this.list.splice(0, 1)[0]);
    console.log(this.list);
  }
}

var ct = new My_controller("http://");
ct.order_list();

Meanwhile this is not the best choice for optimizing the code, is it? So I was wondering why is it not possible to get that variable and how to solve this?

I know one choice would be to save it inside this like:

function My_controller(url) {
  this.list = [1, 2, 3];
  this.url = url;
}

And then accessing this.url, instead of url.

Can you think on a better way to do this?

8
  • 2
    there is no way to do it. you can find it here. stackoverflow.com/questions/436120/… Commented Oct 21, 2016 at 11:13
  • 1
    Why don't you want to save url in My_controller? It makes sense to save it there as a property if you want to use it in your controller's methods. Commented Oct 21, 2016 at 11:14
  • 1
    You are needlessly getting lost in the weeds here. If you are so concerned about information hiding that you feel the need for 'private' variables (which JS doesn't even have) then you don't use prototypes. How many of these things are you really creating anyway? Is this optimization (use of prototype methods to avoid allocation of function objects) saving you megabytes or just bytes? Commented Oct 21, 2016 at 11:36
  • This is due to inheritance, really Commented Oct 21, 2016 at 11:40
  • @Reason its possible in ES6+, see my answer. Commented Oct 21, 2016 at 11:56

1 Answer 1

1

This can be accomplished in ES 6, but I still think its largely unnecessary except at the module-level:

const Foo = (() => {
  let privateData = new WeakMap();

  return class {
    constructor () {
      privateData.set(this, {
        hidden: 'bar'
      });
    }

    // this is a prototype function, we could equivalently define
    // it outside the class definition as Foo.prototype.getPrivate
    // and it would be the same  
    getPrivate () {
      return privateData.get(this).hidden;
    }
  }
})();

console.log(new Foo().getPrivate()); // logs 'bar'

Importance of the WeakMap:

It allows arbitrary objects as keys, but those keys are weakly held: they don't count as references to prevent the key object from being garbage collected. Once the class is returned, code outside the IIFE its defined in can't access privateData. See this for more info.

Note that this won't necessarily break inheritance depending on how you're doing it:

class Baz extends Foo {
  constructor () {
    super(); // all good thanks to super
  }
}

// using non ES-6
function Foo () {
  privateData.set(this, {...});
}

Foo.prototype.getPrivate = function() {
  return privateData.get(this); 
});

function Bar() {
  Foo.call(this); // fine here
}

Bar.prototype = Foo.prototype;

The only limitation is that anything you want to be able to access the private data has to be defined inside the IIFE, you can't come along later and add a method to Foo's prototype. Although, if you're that concerened with performance, you shouldn't be altering prototypes willy-nilly anyway.

Sign up to request clarification or add additional context in comments.

2 Comments

privateData is equal to undefined in prototype object (non ES6)
@TheReason that's the entire point, privateData is private and held in the WeakMap in the closure. Otherwise, if it were attached to the prototype, it'd be public. The point of my answer is that the getPrivate method is shared, each instance doesn't need its own copy to access the closure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.