- Mastering JavaScript Design Patterns
- Simon Timms
- 193字
- 2021-08-05 17:14:57
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.
- .NET之美:.NET關鍵技術深入解析
- Spring Boot開發與測試實戰
- Redis Applied Design Patterns
- Visual Basic編程:從基礎到實踐(第2版)
- PhoneGap Mobile Application Development Cookbook
- Python完全自學教程
- Visual C#.NET程序設計
- UML 基礎與 Rose 建模案例(第3版)
- 西門子S7-200 SMART PLC編程從入門到實踐
- HTML+CSS+JavaScript網頁設計從入門到精通 (清華社"視頻大講堂"大系·網絡開發視頻大講堂)
- Python預測之美:數據分析與算法實戰(雙色)
- Java高手是怎樣煉成的:原理、方法與實踐
- 數字媒體技術概論
- Arduino Electronics Blueprints
- HTML5+CSS+JavaScript深入學習實錄