Created
March 17, 2018 08:00
-
-
Save linx4200/b665985d7df3160ba27d83c9677e0ab6 to your computer and use it in GitHub Desktop.
【继承】构造函数继承
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 SuperClass(id) { | |
this.id = id; | |
this.books = ['a' ,'b', 'c']; | |
} | |
SuperClass.prototype.showBooks = function () { | |
console.log(this.books); | |
} | |
function SubClass (id) { | |
SuperClass.call(this, id); | |
} | |
var instance1 = new SubClass(10); | |
var instance2 = new SubClass(11); | |
instance1.books.push('d'); | |
console.log(instance1.books); // ['a' ,'b', 'c', 'd'] | |
console.log(instance1.id); // 10 | |
console.log(instance2.books); // ['a' ,'b', 'c'] | |
console.log(instance2.id); // 11 | |
instance1.showBooks(); // TypeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment