воскресенье, 17 июня 2012 г.

Java Script Patterns

http://addyosmani.com/resources/essentialjsdesignpatterns/book/#modulepatternjavascript


Creational Design Patterns

The three common ways to create new objects in JavaScript are as follows:
var newObject = {};
var newObject = Object.create( null );
var newObject = new Object();

Constructors With Prototypes

function Car( model, year, miles ) {

  this.model = model;
  this.year = year;
  this.miles = miles;

}


// Note here that we are using Object.prototype.newMethod rather than 
// Object.prototype so as to avoid redefining the prototype object
Car.prototype.toString = function () {
  return this.model + " has done " + this.miles + " miles";
};

// Usage:

var civic = new Car( "Honda Civic", 2009, 20000 );
var mondeo = new Car( "Ford Mondeo", 2010, 5000 );

console.log( civic.toString() );
console.log( mondeo.toString() );


Structural Design Patterns


Behavioral Design Patterns



Комментариев нет: