[TypeScript] 1. Catching JavaScript Mistakes with TypeScript
The TypeScript compiler is a powerful tool which catches mistakes even in vanilla JavaScript. Try it online at the TypeScript Playground, zero setup required.
Error version:
var movie = { title: "Memento", year: 2000, IMDB: 8.5, title: "" };
var rating = movie.imdb;
function Point(x, x) {
this.x = x;
this.y = y;
}
Point.prototype.distance = function() {
return Math.sqrt(x * x + y * y);
};
function isPast(date) {
var now = Date().getTime();
return date.getTime() < now;
}
function ask(question) {
var answer = confirm(question);
var answerView = document.getElementByID('answer');
answerView.innerHTML = answer;
}
function handleLoad() { console.log('loaded'); }
document.onLoad = handleLoad();
function newCoinToss() {
return Math.random > 0.5 ? 'HEADS' : 'TAILS';
}
var tosses = [1,2,3].map(newCoinToss);
var allHeads = tosses.every(function(toss) {
return toss = 'HEADS';
});
if (allHeads) console.log(allHeads.length, 'heads in a row!');
document.addEventListener('keydown', function(event) {
console.log(event.clientX, event.clientY);
});
Using WebStorm, in the Terminal, it displays the error message for you.
Fixed version in TypeScript:
var movie = { title: "Memento", year: 2000, IMDB: 8.5 };
var rating = movie.IMDB;
function Point(x,y) {
this.x = x;
this.y = y;
}
Point.prototype.distance = function() {
return Math.sqrt(this.x * this.x + this.y * this.y);
};
function isPast(date) {
var now = new Date().getTime();
return date.getTime() < now;
}
function ask(question) {
var answer = prompt(question);
var answerView = document.getElementById('answer');
answerView.innerHTML = answer;
}
function handleLoad() { console.log('loaded'); }
document.onload = handleLoad;
function newCoinToss() {
return Math.random() > 0.5 ? 'HEADS' : 'TAILS';
}
var tosses = [1,2,3].map(newCoinToss);
var allHeads = tosses.every(function(toss) {
return toss == 'HEADS';
});
if (allHeads) console.log(tosses.length, 'heads in a row!');
document.addEventListener('mousedown', function(event) {
console.log(event.clientX, event.clientY);
});
[TypeScript] 1. Catching JavaScript Mistakes with TypeScript的更多相关文章
- 玩转TypeScript(引言&文章目录) --初看TypeScript.
JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...
- 使用TypeScript如何提升JavaScript编程效果?
TypeScript是个什么鬼?和JavaScript有什么关系? TypeScript是由微软开发的一种可快速入门的开源的编程语言,是JavaScript的一个超集,且向这个语言添加了可选的静态类型 ...
- 使用Typescript来写javascript
使用Typescript来写javascript 前几天尝试使用haxejs来写javascript,以获得静态类型带来的益处.虽然成功了,但很快发现将它与angularjs一起使用,有一些不太顺畅的 ...
- eval5: TypeScript编写的JavaScript解释器
eval5是基于TypeScript编写的JavaScript解释器,100%支持ES5语法. 项目地址:https://github.com/bplok20010/eval5 使用场景 浏览器环境中 ...
- electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...
- 分享:使用 TypeScript 编写的 JavaScript 游戏代码
<上篇博客>我写出了我一直期望的 JavaScript 大型程序的开发模式,以及 TS(TypeScript) 的一些优势.博客完成之后,我又花了一天时间试用 TS,用它来重构之前编写的一 ...
- CoffeeScript?TypeScript?还是JavaScript
请注意本文只是我的偏见,我努力地理解借助CoffeeScript或TypeScript之类的编译器写JavaScript代码的理由.静态编译.强类型语言和框架,我有着这些流行的.丰富的背景.我的上一份 ...
- 在TypeScript中扩展JavaScript基础对象的功能
最近工作中用到,记录一下:假设我们需要一个功能,把一个数字比如10000输出为下面的字符串格式“10,000”,一般是写一个方法,那么我希望更方便一点,直接向Number类型添加一个格式化方法,比如叫 ...
- [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript
Sometimes we might want to make a function more generic by having it accept a union of different typ ...
随机推荐
- Android开发规范
一.Android编码规范 1.java代码中不出现中文,最多注释中可以出现中文 2.局部变量命名.静态成员变量命名 只能包含字母,单词首字母出第一个外,都为大写,其他字母都为小写 3.常量命名 只能 ...
- 【HDOJ】1098 Ignatius's puzzle
数学归纳法,得证只需求得使18+ka被64整除的a.且a不超过65. #include <stdio.h> int main() { int i, j, k; while (scanf(& ...
- access to modified closure 闭包的问题
; i < listBoxDevices.Items.Count; i++) { var tempDeviceId = listBoxDevices.Items[i].ToString(); i ...
- BZOJ_1180_[CROATIAN2009]_OTOCI_(LCT)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1180 三种操作: 1.询问x,y是否连通,如果不连通,建一条边x,y 2.把x节点的权值改为t ...
- HDU5673 Robot 默慈金数
分析: 注:然后学了一发线性筛逆元的姿势 链接:http://blog.miskcoo.com/2014/09/linear-find-all-invert #include<iostream& ...
- [Irving]SQL去重复-DISTINCT用法
在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出不同(distinct)的值.关键词 distinct用于返回唯一不同的值. 表A: 示例1 select distinct nam ...
- 使用页面对象模型(pageFactory)
页面对象模型可以使测试脚本有更高癿可维护性,减少了重复癿代码,把页面抽象出来. 页面对象设计模式提供了测试一个接口,测试可以像用户行为一样来操作页面. 通过隐藏页面元素定位,返有劣将测试代码和页面分离 ...
- 在今天,我们为什么还要做一个CMS
我们今天看到,在这个移动大潮席卷来的这几年,互联网以惊人的速度改变着这个世界.包括我们这个在中国互联网史上有重大影响力的“站长”,也几乎全军覆没.当然随着站长们兴起的开源CMS,到今天也都穷途末路了. ...
- 定时备份为Sharepoint做网站备份,并删除指定日期的备份
一.创建bat文件 @echo cd \ c: cd "Program Files\Common Files\Microsoft Shared\web server extensions\1 ...
- ubuntu下git安装及连接github
1.安装 sudo apt-get install git git-core git-gui git-doc git-svn git-cvs gitweb gitk git-email git-dae ...