La forma más efectiva de hacerlo (en términos de memoría) es haciendo uso de los prototypes de las funciones tal y como sigue:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Super class | |
var Car = function(loc){ | |
this.loc=loc; | |
}; | |
Car.prototype.move=function(){ | |
this.loc++; | |
}; | |
//Sub class | |
var Van = function (loc){ | |
Car.call(this.loc); | |
} | |
Van.prototype=Object.create(Car.prototype); | |
Van.prototype.constructor=Van; | |
Van.prototype.grab=function(){ | |
//whatever | |
} | |
var zed = new Car(3); | |
zed.move(); | |
var amy=new Van(9); | |
amy.move(); | |
amy.grab(); |
PD: Sacado del curso de Udacity-Object Oriented Javascript (https://www.udacity.com/course/ud015), si puedes hazlo.
2 comentarios:
As always your articles do inspire me. Every single detail you have posted was great. ExcelR Machine Learning Course
Publicar un comentario