官术网_书友最值得收藏!

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/).

主站蜘蛛池模板: 沂源县| 饶河县| 固安县| 安乡县| 黔西| 吴川市| 东乡族自治县| 拉萨市| 朔州市| 泗阳县| 保靖县| 邛崃市| 甘孜县| 当雄县| 邻水| 久治县| 霍山县| 章丘市| 梅河口市| 衡水市| 汉沽区| 安国市| 临邑县| 马鞍山市| 恩平市| 公安县| 巢湖市| 柯坪县| 新田县| 瓦房店市| 墨脱县| 石家庄市| 子长县| 民权县| 山东| 日照市| 磴口县| 南靖县| 瑞丽市| 海伦市| 揭阳市|