Skip to content

Instantly share code, notes, and snippets.

@linx4200
Created March 17, 2018 08:00
Show Gist options
  • Save linx4200/b665985d7df3160ba27d83c9677e0ab6 to your computer and use it in GitHub Desktop.
Save linx4200/b665985d7df3160ba27d83c9677e0ab6 to your computer and use it in GitHub Desktop.
【继承】构造函数继承
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