Bal*_*ala 10 javascript typescript
我刚刚开始阅读Typescript的基础知识,谁能告诉我使用Typescript优于Javascript或jquery的优势?一些例子会有所帮助.
lhk*_*lhk 14
我已经使用了一些小型爱好项目的打字稿,我确信它是完美的Javascript替代品.我最喜欢的功能是:
声明文件.使用声明文件,您可以向javascript库添加类型信息.这些结构信息将为您在VisualStudio中提供出色的智能感知支持.
"标准"OOP.如果你来自C#或Java背景,你可能甚至不需要打字稿教程.它只是有效.你有类,接口,访问修饰符,扩展机制,......
内置模块支持.Typescript有一个稍微混乱的模块系统.您可以将代码拆分为几个.ts文件,只需附加它们,但您也可以创建不同的模块.
最后:语法.有时它是影响最大的小事.对我来说,打字稿的语法感觉非常完美.我举几个例子:
使用":"键入注释并键入推断
var a = 5; //infers that a has the type number
var canvas : HTMLCanvasElement = document.getElementById("canvas");
// automatically casts to the canvas type. Intellisense will now suggest all the canvas specific methods
Run Code Online (Sandbox Code Playgroud)
作为列表,堆栈,...的数组
var array= []; //dynamic type
array.push(1);
array[1]=2;
array.pop();
var array2 : number[] = []; //typed array
array[0]=2;
array[1]="hello" //compile time error. You've got to love the type system. Finally you can trust your collections
Run Code Online (Sandbox Code Playgroud)
使用箭头语法作为lambdas的函数:
var array=[];
array.push(1);
//...
array.forEach((num)->{alert(num);});
//for single statement functions you can write
array.forEach((num)->alert(num));
Run Code Online (Sandbox Code Playgroud)
现在键入的数组和lambdas组合在一起:
var array: number[]=[];
array.push(1);
//...
//let's assume you want to work with the data in the array. You've got to filter it and process it. Lambdas will come in handy, as well as the type inference
array.filter((num)->num>3).map((num)->num*2).forEach((num)->alert(num));
// the first lambda with the comparison is fully type safe. The compiler knows the type of the array. Therefore it can infer the type of the parameter num and will check if num can be compared to a number
Run Code Online (Sandbox Code Playgroud)
我非常喜欢使用打字稿.它将显着提高您的生产力.还有更多内容:http://typescript.codeplex.com/wikipage?title = Routemap
0.9版本将提供泛型,对于1.x版本,他们计划实现async/await调用.
以下是有关 TypeScript 的视频集合:
http://channel9.msdn.com/search?term=typescript
基本上它为 Javascript 添加了可选的静态类型,因此静态类型的所有优点都给 Javascript 带来了。
| 归档时间: |
|
| 查看次数: |
9118 次 |
| 最近记录: |