Last active
August 29, 2015 14:05
-
-
Save DanilloCorvalan/11605eade0e3e508a9c2 to your computer and use it in GitHub Desktop.
Prototype inheritance and Chain // source http://jsbin.com/dukehe/1
This file contains hidden or 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
function Person (name, surname) { | |
this.name = name; | |
this.surname = surname; | |
} | |
Person.prototype.showFullName = function () { | |
return this.name + ' ' + this.surname; | |
}; | |
function Employee (name, surname, company) { | |
this.company = company; | |
//chama o construtor do Person aplicando o context atual do Employee | |
Person.call(this, name, surname); | |
} | |
Employee.prototype = Object.create(Person.prototype); | |
Employee.prototype.constructor = Employee; | |
var rafael = new Person('Rafael', 'Demenech'); | |
var danillo = new Employee ('Danillo', 'Corvalan', 'Bravi'); | |
//rafael.company é undefined porque é criado apartir de Person | |
//e não de Employee | |
console.log(rafael.name, rafael.surname, rafael.company); | |
console.log(rafael.showFullName()); | |
//danillo tem a prop company porque é criado apartir do construtor Employee | |
console.log(danillo.name, danillo.surname, danillo.company); | |
console.log(danillo.showFullName()); | |
//testando prototype chain dos objetos | |
//rafael é uma instancia de Person mas NÃO de Employee | |
console.log(rafael instanceof Person); | |
console.log(rafael instanceof Employee); | |
//danillo é uma instancia de Employee e Person = "Prototype Chain"" | |
console.log(danillo instanceof Person); | |
console.log(danillo instanceof Employee); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment