// constructor
var ParentClass = function () {
// constructor
this.defaultName = 'Avétis';
};
ParentClass.prototype.sayHelloTo = function (name) {
console.log('Hi ' + (name || this.defaultName) + '!');
};
var ChildClass = function () {
// optional, only if you want to call parent constructor
ChildClass.prototype.constructor.apply(this, arguments);
};
// inheritance
ChildClass.prototype = Object.create(ParentClass.prototype);
ChildClass.prototype.sayHelloTo = function (name) {
name = name || this.defaultName;
name = name[0].toUpperCase() + name.slice(1);
// call parent class method
ChildClass.prototype.constructor.prototype.sayHelloTo.call(this, name);
};
// instanceof
var a = new ParentClass();
var b = new ChildClass();
console.log(b instanceof ChildClass); // => true
console.log(b instanceof ParentClass); // => true
console.log(a instanceof ChildClass); // => false
console.log(a instanceof ParentClass); // => true
console.log(a.sayHelloTo()); // => "Hi Avétis!"
console.log(b.sayHelloTo()); // => "Hi Avétis!"
console.log(a.sayHelloTo('avetisk')); // => "Hi avetisk!"
console.log(b.sayHelloTo('avetisk')); // => "Hi Avetisk!"
Alguns podem querer adicionar a classe pai ChildClass.__super__ = ParentClass
para facilitar o acesso (o que eu nĂŁo recomendo).