Vandervals Vandervals - 1 month ago 11
Javascript Question

How to get private variables from prototyped methods in Javascript

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?

Answer

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.

Comments