实际上,确实可以为一个原型创建多个构造函数。你只要看看
constructor
原型的属性
is not special
. 因此,可以创建多个构造函数,如下所示:
const type = (prototype, constructor) =>
(constructor.prototype = prototype, constructor);
const book = {
flipTo(page) {
this.page = page;
},
turnPageForward() {
this.page++;
},
turnPageBackward() {
this.page--;
}
};
const EmptyBook = type(book, function EmptyBook() {
this.constructor = EmptyBook;
this.ISBN = "";
this.title = "";
this.author = "";
this.genre = "";
this.covering = "";
this.length = -1;
this.page = 0;
});
const Book = type(book, function Book(title, author, length) {
this.constructor = Book;
this.ISBN = "";
this.title = title;
this.author = author;
this.genre = "";
this.covering = "";
this.length = length;
this.page = 0;
});
只需给构造函数起不同的名字。希望能有所帮助。