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

ECMAScript 6 classes and modules

We've seen so far that it is perfectly possible to build classes and even modules in JavaScript. The syntax is, obviously, a bit more involved than in a language such as C# or Java. Fortunately, the next version of JavaScript, ECMAScript 6 (also known as Harmony), brings support for some syntactic sugar for making classes:

class Castle extends Westeros.Structures.BaseStructure {
  constructor(name, allegience) {
    super(name);
    ...
  }

  Build() {
    ...
    super.Build();
  }
}

ECMAScript 6 also brings a well thought out module system for JavaScript. There is syntactic sugar for creating modules which looks like:

module 'Westeros' {
    export function Rule(rulerName, house) {
     ...
        return "Long live " + rulerName + " of house " + house;
    }
}

As modules can contain functions, they can, of course, contain classes. ECMAScript 6 also defines a module import syntax, and support for retrieving modules from remote locations. Importing a module looks like this:

import westeros from 'Westeros';
module JSON from 'http://json.org/modules/json2.js';
westeros.Rule("Rob Stark", "Stark");

Some of this syntactic sugar is available with current JavaScript, but it does require some additional tooling, which is outlined in Chapter 12, ES6 Solutions Today.

主站蜘蛛池模板: 分宜县| 安多县| 毕节市| 那坡县| 弋阳县| 大庆市| 关岭| 略阳县| 长沙市| 宝丰县| 遂溪县| 桦甸市| 平凉市| 长宁区| 留坝县| 琼中| 荥阳市| 来宾市| 广西| 浏阳市| 韶山市| 封开县| 荆州市| 灵山县| 蒙山县| 霍州市| 东港市| 漳州市| 成武县| 新兴县| 龙陵县| 双柏县| 双桥区| 乐山市| 平昌县| 沾益县| 江永县| 石城县| 航空| 镇康县| 景洪市|