factory/constructor

factory provided by a closure that also houses constructor and private pseudo method

by phloe

JavaScript

var Constructor = (function () {

  function C (name, value) {
    this.name = name;
    this.value = value; 
  };

  C.prototype = {

    method: function () {
      return privateMethod.call(this);
    }

  };
  
  // private pseudo method 
  function privateMethod () {
    return btoa(this.value);
  }
  
  return C;

}());

var factory = function (name, value) {
  return new Constructor(name, value);
};

var A = factory("foo", "bar");

if (window.console) {
  console.log("data", A.method());
}