Creating Objects With The Functional Pattern


function extend(destination, source) {
  for (var k in source) {
    if (source.hasOwnProperty(k)) {
      destination[k] = source[k];
    }
  }
  return destination; 
}

var Person = function(age) {
  var obj = {age: age};
  extend(obj, Person.methods);
  return obj;
};

Person.methods = {  
  ageing : function(){
    this.age++;
  }
};

var ben = new Person(26);
ben.ageing();
console.log(ben.age); // -> 27

Add your comment