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:
varMovie = {title: "Memento", year:2000, imdb:8.5, title: "" };varRating =Movie.imdb;functionPoint (x, x) { This. x =x; This. y =y;} Point.prototype.distance=function() { returnMATH.SQRT (x * x + y *y);};functionIspast (date) {varnow =Date (). GetTime (); returnDate.gettime () <Now ;}functionAsk (question) {varAnswer =confirm (question); varAnswerview = document.getElementById (' answer ')); Answerview.innerhtml=answer;}functionHandleload () {console.log (' loaded ')); }document.onload=handleload ();functionNewcointoss () {returnMath.random > 0.5? ' HEADS ': ' TAILS '; }vartosses = [A].map (newcointoss);varAllheads = Tosses.every (function(Toss) {returnToss = ' HEADS '; });if(allheads) Console.log (Allheads.length, ' heads in a row! ');d Ocument.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:
varMovie = {title: "Memento", year:2000, imdb:8.5 };varRating =movie. IMDB;functionPoint (x, y) { This. x =x; This. y =y;} Point.prototype.distance=function() { returnMATH.SQRT ( This. x * This. x + This. Y * This. y);};functionIspast (date) {varnow =NewDate (). GetTime (); returnDate.gettime () <Now ;}functionAsk (question) {varAnswer =prompt (question); varAnswerview = document.getElementById (' answer ')); Answerview.innerhtml=answer;}functionHandleload () {console.log (' loaded ')); }document.onload=Handleload;functionNewcointoss () {returnMath.random () > 0.5? ' HEADS ': ' TAILS '; }vartosses = [A].map (newcointoss);varAllheads = Tosses.every (function(Toss) {returnToss = = ' HEADS '; });if(allheads) Console.log (Tosses.length, ' heads in a row! ');d Ocument.addeventlistener (' MouseDown ',function(event) {Console.log (Event.clientx, Event.clienty);});
[TypeScript] 1. Catching JavaScript mistakes with TypeScript