Learning Ext JS 3

Inheritance in JavaScript Más videos

Descripción del tema

JavaScript has a unique way of object creation and inheritance, this concept is called "prototypal inheritance", basically an object can inherit methods and properties to other objects creating a prototype to create new objects. The prototypal inheritance can be done by using the property "prototype" that you can find in all the objects. In JavaScript the inheritance is simple but with some effort you can obtain the multiple inheritance; in this chapter we will show how to do a simple inheritance. It is important to remember that the property "prototype" can only inherit from other objects and not prototypes or constructors. Let's see the next example to understand this better:
//Super "class"
var Animal = function(type){
	this.type = type;
}

Animal.prototype.getType = function(){
	return this.type;
}

var Dog = function(options){
	this.breed = options.breed;
}

//Inheritance
Dog.prototype = new Animal('Dog');

//attach methods to the Dog "class"
Dog.prototype.run = function(){
	console.debug('the '+this.breed+' '+this.type+' is running!');
}
//new instance
var beagle = new Dog({breed:'Beagle'});

//calling a method of the super "class"
console.debug(beagle.getType());
beagle.run();
The most important part of the previous example is where we do the inheritance, “Dog.prototype = new Animal('Dog');”. The variable Dog makes a reference to the constructor of the object Dog, “new Animal()” is creating an Animal object which is assign to the prototype of the constructor of the "Dog" object; in this way the Dog object will have all the methods and properties of the Animal object when new instances are created.

Conclusion

The simple inheritance is something we will use when we develop components, that's why is important to understand this concept.

Te gustaría recibir más tutoriales como este en tu correo?

Este tutorial pertenece al curso Learning Ext JS 3, te recomiendo revises el resto de los tutoriales ya que están en secuencia de menor a mayor complejidad.

Si deseas recibir más tutoriales como este en tu correo te recomiendo registrarte al curso, si ya eres miembro solo identifícate y registrate al curso, si no eres miembro te puedes registrar gratuitamente!

Si no gustas registrarte en este momento no es necesario! Aún así puedes recibir los nuevos tutoriales en tu correo! Jamás te enviaremos Spam y puedes cancelar tu suscripción en cualquier momento.

¿Olvidaste tu contraseña?

1Comentario

  • Avatar-6 Aditya 09/09/2010

    This one would have been more elaborative and informative if we have an example of subclass overriding the super method and then calling super's method even when it is overridden. I believe, that would bring in good shape to this tutorial.

    Instructor del curso

    Crysfel3

    Autor: Crysfel Villa

    Software engineer with more than 7 years of experience in web development.

    Descarga Código Fuente

    Regístrate a este curso

    Este tutorial pertenece al curso Learning Ext JS 3, revisa todos los tutoriales que tenemos en este mismo curso ya que están en secuencia y van de lo más sencillo a lo más complicado.

    Tendrás acceso a descargar los videos, códigos y material adicional.

    Podrás resolver los ejercicios incluidos en el curso así como los Quizzes.

    Llevarás un registro de tu avance.