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

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.

主站蜘蛛池模板: 城固县| 田东县| 望江县| 勐海县| 伊宁县| 荔波县| 和政县| 平罗县| 楚雄市| 伊川县| 神农架林区| 红河县| 石渠县| 远安县| 宁波市| 临漳县| 察雅县| 重庆市| 宣城市| 平罗县| 永春县| 郁南县| 九龙县| 嵊泗县| 莒南县| 贡山| 宜都市| 彩票| 兰考县| 亚东县| 延边| 南安市| 郑州市| 濮阳县| 灵璧县| 绥芬河市| 大冶市| 襄汾县| 平乡县| 滁州市| 普陀区|