Return to Snippet

Revision: 43134
at January 5, 2012 00:40 by coprolit


Updated Code
/*
 The idea here is that you have private methods
 which you want to expose as public methods.
*/

var SomeModule = (function(param){
	// Private stuff
	// 'this' keyword refers to the current instance. Must use 'new' when creating instances of the module, else 'this' will refer to global scope.
	this.initval = param;
	this.name = 'John Smith';
	this.age = 40;

	function setPerson(param) {
		this.name = param;
	}
		
	function getPerson() {
		return this.name; // create closure (inner function) to bind the var (= keep it around)
	}
	// Public stuff / reveal and bind
	return {
		set: this.setPerson,
		get: this.getPerson,
		age: this.age
	};
});

// creating instances
var newModule = new SomeModule(newparam);
newModule.set("Some Name");
var name = newModule.age;
var name = newModule.get;

Revision: 43133
at March 17, 2011 20:02 by coprolit


Updated Code
/*
 The idea here is that you have private methods
 which you want to expose as public methods.
*/

var myRevealingModule = function(){
  //private attributes
  var privateVar = 5;
  
  //private methods
  function setVar(arg)
  {
    privateVar = arg;
  };

  function getVar()
  {
    return privateVar;
  };
  
  //revealing public API aka public methods
  return
  {
    set: setVar,
    get: getVar
  }

}();

// Sample usage:
myRevealingModule.set(7);
myRevealingModule.get();

Revision: 43132
at March 17, 2011 20:01 by coprolit


Updated Code
/*
 The idea here is that you have private methods
 which you want to expose as public methods.
*/

var myRevealingModule = function(){
  //private attributes
  var privateVar = 5;
  
  //private methods
  function setVar(arg){
    privateVar = arg;
  };

  function getVar(){
    return privateVar;
  };
  
  //public methods
  return{
    set: setVar,
    get: getVar
  }

}();

// Sample usage:
myRevealingModule.set(7);
myRevealingModule.get();

Revision: 43131
at March 17, 2011 09:11 by coprolit


Updated Code
/*
 The idea here is that you have private methods
 which you want to expose as public methods.
*/

var myRevealingModule = function(){
  //private attributes
  var privateVar = 5;
  
  //private methods
  function setVar(){
    var privateVar = 10;
  };

  function getVar(){
    return privateVar;
  };
  
  //public methods
  return{
    set: setVar,
    get: getVar
  }

}();

// Sample usage:
myRevealingModule.get();

// Douglas Crockford style:

var myModule = (function(){
  
  //private attributes
  var privateVar = 5;
  
  //private methods
  var privateMethod = function(){
    return 'Private Test';
  };
  
  return {

    //public attributes
      publicVar    : 10,

    //public methods
    publicMethod : function(){
      return ' Followed By Public Test ';
    }, 

    //let's access the private members
    getData : function(){
      return privateMethod() + this.publicMethod() + privateVar;
    }
  }

})(); //the parens here cause the anonymous function to execute and return

myModule.getData();

Revision: 43130
at March 17, 2011 08:40 by coprolit


Initial Code
var someModule = (function(){
  
  //private attributes
  var privateVar = 5;
  
  //private methods
  var privateMethod = function(){
    return 'Private Test';
  };
  
  return {

    //public attributes
      publicVar    : 10,

    //public methods
    publicMethod : function(){
      return ' Followed By Public Test ';
    }, 

    //let's access the private members
    getData : function(){
      return privateMethod() + this.publicMethod() + privateVar;
    }
  }

})(); //the parens here cause the anonymous function to execute and return

someModule.getData();

Initial URL
http://www.addyosmani.com/resources/essentialjsdesignpatterns/book/#designpatternsjavascript

Initial Description
"Javascript module pattern emulates the concept of classes in such a way that we're able to include both public/private methods and variables inside a single object, thus shielding/namespacing particular parts from the global scope."
When inheritance is not needed and a only few instances is needed (Keep in mind that each instance places a new copy of each function in memory!)

Initial Title
Javascript revealing module pattern template

Initial Tags
javascript, class, object, template, module

Initial Language
JavaScript