lunes, 27 de abril de 2015

JavaScript - Prototype

//La propiedad prototype permite añadir nuevas propiedades a un prototipo existente: 
function person(first, last, age, eye) {
 this.firstName = first;
 this.lastName = last;
 this.age = age;
 this.eyeColor = eye;
}
person.prototype.nationality = "English";

var myFather = new person("John", "Doe", 50, "blue");
  
console.log ("My father is " + myFather.nationality); //My father is English


//La propiedad prototype también permite añadir nuevos métodos a un prototipo existente:
function person(first, last, age, eye) {
    this.firstName = first;
    this.lastName = last;
    this.age = age;
    this.eyeColor = eye;
}
person.prototype.name = function() {
    return this.firstName + " " + this.lastName
};

var myFather = new person("John", "Doe", 50, "blue");

console.log("My father is " + myFather.name()); //My father is John Doe

No hay comentarios:

Publicar un comentario