转载

TypeScript新功能实例讲解:支持更多EcmaScript6特性

TypeScript 1.5 Alpha正式发布,这是TypeScript 1.5的预览版。此版本增加许多新功能。

注* 在这篇文章中我们会为每个ts示例附加所编绎出的js文件。通过NPM安装TypeScript编绎器,并编绎ts文件:

//全局安装
npm install typescript -g
//编绎TS文件
tsc helloworld.ts

更多的ES6功能的支持

在TypeScript 1.5中,我们加入了大量新的 ES6 功能。这些类型系统将能让使用新的 ES6 代码模式。

模块(Modules)

ES6 的模块语法是实现模块化的有力途径。通过导入整个模块或个别接口,您可以实现与模块的交互。

ES6允许你只导出、导入特定的功能,你还可以在模块功能上面添加default, 比如:

// math.ts

export function add(x, y) { return x + y }
export function subtract(x, y) { return x – y }
export default function multiply(x, y) { return x * y }

// myFile.ts

//只导入math中的add,subtract方法
import {add, subtract} from "math";
//将math中的默认导出方法命名为times
import times from "math";
var result = times(add(2, 3), subtract(5, 3));

如果您一直在使用TypeScript,你可能会注意到有关于TypeScript外部模块(external modules)的报错。这并不意外。当我们为TypeScript创建外部模块时,我们正在对解决这个问题。我们鼓励开发人员使用更强的 ES6 模块语法。

注* 通过编绎出的结果可以看出export的实现原理:

// math.js

function add(x, y) { return x + y; }
exports.add = add;
function subtract(x, y) { return x - y; }
exports.subtract = subtract;
function multiply(x, y) { return x * y; }
exports.default = multiply;

// myFile.js

var math_1 = require("math");
var math_2 = require("math");
var result = math_2.default(math_1.add(2, 3), math_1.subtract(5, 3));

解构(Destructuring)

解构是ES6的新功能。有了它,你可以分开声明对象或数组的对象。

// 解构声明一个数组, x对应10, y对应20
var [x, y] = [10, 20];
[x, y] = [y, x]; // 简单的交换

// 你还可以在函数传入参数中使用此声明
var myClient = {name: "Bob", height: 6};
function greetClient({name, height: howTall}) {
console.log("Hello, " + name + ", who is " + howTall + " feet tall.");
}
greetClient(myClient);

传入的对象含有name和height属性,通过此语言,height将被重命名为howTall变量;

注* 编绎出来的结果,你可能会注意到_b变量声明被放到了最后,实际输出结果确实如此,但JS会自动把声明提升,因此不会引起任何问题。

var _a = [10, 20], x = _a[0], y = _a[1];
_b = [y, x], x = _b[0], y = _b[1]; // 简单的交换


var myClient = { name: "Bob", height: 6 };
function greetClient(_a) {
var name = _a.name, howTall = _a.height;
console.log("Hello, " + name + ", who is " + howTall + " feet tall.");
}
greetClient(myClient);
var _b;

更多

我们同样为枚举添加了 for-of 支持, let/const 声明, unicode 支持, 和对 computed properties 更好的支持。

修饰符(Decorators)

我们也采纳了Angular,Ember和Aurelia (Durandal的制造商) 团队的建议,添加了 ES7 新的修饰符的建议。新的修饰符声明起来更加简洁。在此示例中,我们看到 @memoize 装饰应用在 getter/setter 上的例子:

class Person {
@memoize
get name() { return `${this.first} ${this.last}` }

set name(val) {
let [first, last] = val.split(' ');
this.first = first;
this.last = last;
}
}

注* memoize 是指记住(缓存)某些输入参数的输出结果,提高响应速度,下面是编绎出的结果:

var __decorate = this.__decorate || function (decorators, target, key, value) {
var kind = typeof (arguments.length == 2 ? value = target : value);
for (var i = decorators.length - 1; i >= 0; --i) {
var decorator = decorators[i];
switch (kind) {
case "function": value = decorator(value) || value; break;
case "number": decorator(target, key, value); break;
case "undefined": decorator(target, key); break;
case "object": value = decorator(target, key, value) || value; break;
}
}
return value;
};
var Person = (function () {
function Person() {
}
Object.defineProperty(Person.prototype, "name", {
get: function () { return this.first + " " + this.last; },
set: function (val) {
var _a = val.split(' '), first = _a[0], last = _a[1];
this.first = first;
this.last = last;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Person.prototype, "name", __decorate([memoize], Person.prototype, "name", Object.getOwnPropertyDescriptor(Person.prototype, "name")));
return Person;
})();

原文地址:点此

正文到此结束
Loading...