JavaScript Object pattern

by yong

JavaScript

var Object = function(settings) {
    this.name = 'object';
    
    this.init = function(settings) {
        for(var k in settings) {
            this[k] = settings[k];
        }
    }
    this.init(settings);
};

var A = function() {
    Object.apply(this, arguments);
    
    this.name = 'A';
};

A.prototype = Object;

var B = function() {
    A.apply(this, arguments);
    
    this.name = 'B';
};

B.prototype = A;

var a = new A({ x: 1, y: 1, speed: 10 });
var b = new B({ x: 2, y: 2, speed: 20 });

console.log(a)
console.log(b)