October 2018
Intermediate to advanced
590 pages
15h 5m
English
ES2015 introduces classes, which is primarily a syntactical sugar over prototype-based inheritance. With the class syntax, you can create constructors, extends from a superclass, and create static methods, as well as getters and setters.
Let's see the following example that uses the class syntax to implement User, and TeamMember:
1. class User {2. constructor(name, interests) {3. this.name = name;4. this.interests = interests;5. }6. greeting () {7. console.log('Hi, I\'m ' + this.name + '.');8. }9. get interestsCount () {10. return this.interests ? this.interests.length : 0;11. }12. }
In lines 1 to 12, we define class User, which accepts two arguments via its constructor. It has a greeting() method and an interestsCount getter:
Read now
Unlock full access