Skip to content

Instantly share code, notes, and snippets.

@nebiros
Created February 9, 2015 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nebiros/3ff8edd4fb41d5df4483 to your computer and use it in GitHub Desktop.
Save nebiros/3ff8edd4fb41d5df4483 to your computer and use it in GitHub Desktop.
// función constructora intermediaria
function inherits(Child, Parent) {
function I(){}
I.prototype = Parent.prototype;
Child.prototype = new I();
Child.prototype.constructor = Child;
Child.prototype.__super__ = Parent;
}
function Hi(msg) {
this.msg = msg;
}
Hi.prototype = {
constructor: Hi,
say: function (msg) {
return this.msg + " " + msg;
}
}
function MyHi(msg) {
// llamamos al constructor de la class padre, 'Hi' a
// traves del atributo '__super__', este atributo es creado
// en nuestra función 'inherits'
this.__super__.call(this, msg);
}
// heredamos de la clase 'Hi'
inherits(MyHi, Hi);
var hi = new MyHi("mundo");
console.log(hi.say("hola")); // mundo hola
console.log(hi instanceof Hi); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment