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

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

主站蜘蛛池模板: 洛扎县| 泰和县| 神农架林区| 嵊泗县| 昌宁县| 华阴市| 奎屯市| 鸡西市| 如皋市| 鸡东县| 常宁市| 沅江市| 鱼台县| 江陵县| 大田县| 文安县| 花莲市| 曲靖市| 安徽省| 长顺县| 大丰市| 庆阳市| 上饶市| 台南市| 通城县| 衡南县| 基隆市| 依兰县| 喜德县| 霍林郭勒市| 射洪县| 陈巴尔虎旗| 纳雍县| 巩留县| 隆尧县| 嘉祥县| 大姚县| 榆树市| 淮滨县| 安多县| 清涧县|