Posts mit dem Label cheatsheet werden angezeigt. Alle Posts anzeigen
Posts mit dem Label cheatsheet werden angezeigt. Alle Posts anzeigen

Samstag, 23. Juli 2011

JS Module Pattern

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();
SOURCE: http://addyosmani.com/resources/essentialjsdesignpatterns/book/

Mittwoch, 15. Juni 2011

Javascript OOP Cheatsheet

var MyClass = function() {

    // self to get "this" in private / global / anonymous methods
    var self = this;

    // public var
    this.publicVar = 'public';
    // private var
    var privateVar = 'private';

    // public method
    this.publicMethod = function() {}
    // private method
    function privateMethod() {}
    // global method
    globalMethod = function() {}

}