oop in js . is there any difference between to implementing it through the (function returns object way - i belive it called factory function right ?) or using class for implementing it ? like this :
class Publisher {
constructor(name, location) {
this.name = name;
this.location = location;
}
}
class Author {
constructor(name, booksWritten) {
this.name = name;
this.booksWritten = booksWritten;
}
}
class Book {
constructor(title, genre) {
this.title = title;
this.genre = genre;
this.publisher = new Publisher("Sample Publisher", "Somewhere");
this.author = new Author("Sample Author", 5);
}
}
Show the other way
https://youtu.be/dYHWvUVMnCU
the other way you asked for
In your Book class if you accept Publisher and Author in the constructor, that's composition. You have them hard coded in the constructor. That's not composition
you mean as parameters like this ? class Book { constructor(title, genre) { this.title = title; this.genre = genre; this.publisher = new Publisher("Sample Publisher", "Somewhere"); this.author = new Author("Sample Author", 5); } }
no class Book { constructor(title, genre, publisher, author) { this.title = title; this.genre = genre; this.publisher = publisher; this.author = author; } }
how to multi inherit in js
Обсуждают сегодня