- Mastering Angular Components
- Gion Kunz
- 245字
- 2021-07-23 17:23:39
Classes
Classes were among one the most requested features in JavaScript, and I was one of the people voting for it. Well, coming from an OOP background and being used to organizing everything within classes, it was hard for me to let go. Although, after working with modern JavaScript for some time, you'll reduce their use to the bare minimum and to exactly what they are made for—inheritance.
Classes in ECMAScript 6 provide you with syntactic sugar to deal with prototypes, constructor functions, super calls, and object property definitions in a way that you have the illusion that JavaScript could be a class-based OOP language:
class Fruit {
constructor(name) { this.name = name; }
}
const apple = new Fruit('Apple');
As we learned in the previous topic about transpilers, ECMAScript 6 can be de-sugared to ECMAScript 5. Let's take a look at what a transpiler will produce from this simple example:
function Fruit(name) { this.name = name; }
var apple = new Fruit('Apple');
This simple example can easily be built using ECMAScript 5. However, once we use the more complex features of class-based object-oriented languages, the de-sugaring gets quite complicated.
ECMAScript 6 classes introduce simplified syntax to write class member functions (static functions), the use of the super keyword, and inheritance using the extends keyword.
If you would like to read more about the features in classes and ECMAScript 6, I highly recommend that you read the articles of Dr. Axel Rauschmayer (http://www.2ality.com/).
- 通信網絡基礎與設備
- Spring Boot 2.0 Projects
- 互聯網安全的40個智慧洞見:2015年中國互聯網安全大會文集
- Learning Swift(Second Edition)
- Microservice Patterns and Best Practices
- Windows Server 2012 Hyper-V虛擬化管理實踐
- jQuery Mobile Web Development Essentials
- 計算機網絡技術及應用
- 4G小基站系統原理、組網及應用
- 網絡設計與應用(第2版)
- 智慧城市中的物聯網技術
- 數字王國里的虛擬人:技術、商業與法律解讀
- 5G智慧交通
- 從實踐中學習Kali Linux網絡掃描
- Scala Programming Projects