[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 ...
随机推荐
- 【HDU 3038】 How Many Answers Are Wrong (带权并查集)
How Many Answers Are Wrong Problem Description TT and FF are ... friends. Uh... very very good frien ...
- easyui源码翻译1.32--LinkButton(按钮)
前言 使用$.fn.linkbutton.defaults重写默认值对象.下载该插件翻译源码 按钮组件使用超链接按钮创建.它使用一个普通的<a>标签进行展示.它可以同时显示一个图标和文本, ...
- Mpeg-2的同步及时间恢复--STC,PCR,DTS,PTS
http://blog.csdn.net/hice1226/article/details/6717354 Mpeg-2的同步及时间恢复--STC,PCR,DTS,PTS 摘要:Mpeg-2同步及时间 ...
- 【Quick 3.3】资源脚本加密及热更新(三)热更新模块
[Quick 3.3]资源脚本加密及热更新(三)热更新模块 注:本文基于Quick-cocos2dx-3.3版本编写 一.介绍 lua相对于c++开发的优点之一是代码可以在运行的时候才加载,基于此我们 ...
- 浏览器助手,请求拦截,后台模拟键鼠操作,页内嵌入JS
http://www.cnblogs.com/ /// <summary> /// 网页浏览器助手 /// 请求拦截,后台模拟键鼠操作,页内嵌入JS ...
- Android 常用UI控件之TabHost(1)TabHost的两种布局方式
TabHost是Android中的tab组件. TabHost布局文件的基本结构 TabHost下有个layout,这个layout中有TabWidget与FrameLayout.TabWidget是 ...
- INV Close Period & GL Import Journal > DML tables
1. (N) Inventory > Accounting Close Cycle > Inventory Accounting Periods Insert data ...
- 如何配置Java环境
下载JDK并安装 搜索JDK,官网立马就出来了,下载之后个人觉得毕竟开发,毕竟这东西不大,C盘稳一点,安装在C盘可以的 配置 右键打开计算机->属性->高级系统设置->高级-> ...
- BZOJ_1607_ [Usaco2008_Dec]_Patting_Heads_轻拍牛头_(筛数)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1607 给出一组n个数,求每个数能被多少个其他的数整除. 分析 暴力一点的做法就是每个数去筛它的 ...
- easyui 中的treegrid添加checkbox
<script type="text/javascript"> function show(checkid){ var s = '#check_'+checkid; / ...