function Person() { | |
this.firstName = 'Per'; | |
this.lastName = 'son'; | |
this.sayName = function() { return this.firstName + ' ' + this.lastName }; | |
} | |
// Viết một Constructor Function khác | |
function SuperMan(firstName, lastName) { | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
// Ta muốn SuperMan sẽ kế thừa các thuộc tính của Person | |
// Sử dụng prototype để kế thừa | |
SuperMan.prototype = new Person(); | |
// Tạo một object mới bằng Constructor Function | |
var sm = new SuperMan('Hoang', 'Pham'); | |
sm.sayName(); // Hoang Pham. Hàm này kế thừa từ prototype của Person |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment