Compile your Javascript

Compile your Javascript - Hallo friend Insurance WCest, In the article you read this time with the title Compile your Javascript, we have prepared this article well for you to read and take the information in it. hopefully the contents of the post which we write you can understand. okay, happy reading.

Title : Compile your Javascript
Link : Compile your Javascript

Read too


Compile your Javascript

;
I've actually managed to largely avoid having to anything to do with Javascript until I rejoined Google a few months ago. My current project is the Google Maps Javascript API... so I've been learning fast!

Lots of people like to complain about how Javascript is a crappy language, so I was expecting it to be. What I actually found was a pretty decent functional-ish language hidden under the clunky syntax. As the page for Coffeescript put it:

Underneath that awkward Java-esque patina, JavaScript has always had a gorgeous heart.

Indeed, and Coffeescript does do a nice job of exposing that heart. But even in its awkward clothes, and not withstanding some very valid criticisms*, I've found Javascript to be a pretty decent and even a fun programming language. A lot of what people think they hate about Javascript are really things they hate about web programming: the DOM, or browser compatibility hell. In any case, I think most of these other issues pale into Javascript's one big problem: it's weakly typed.

I DO NOT LIKE WEAKLY TYPED PROGRAMMING LANGUAGES.

I should qualify that, of course. Weak typing ins't something that happens by accident: in fact, it requires quite a bit of cleverness in the runtime, and it certainly makes small scripting tasks quite a lot quicker and easier. For scripting languages, I have not problem with weak typing. You may ask: it's called Javascript, so what's the problem then? Well, it may have been envisaged as a scripting language, but for better or for worse Javascript powers the web, and it is being used to build full-scale applications of ever-increasing complexity. From my Python programming days, I know what happens when you try to develop complex full-scale applications in a weakly-typed programming language: bad things happen. Static checking finds far fewer problems than it does for strongly-typed languages, and unless well-considered documentation conventions are rigorously adhered to, figuring out what a piece of code is meant to do can be extremely painful.

Luckily, at Google we have a solution: the Closure Compiler. It is a compiler for Javascript that does all the nice things compilers do (e.g. optimisation, dead-code removal) before outputting a Javascript package that is typically much smaller than whatever you fed into it, and often much faster too. In my travels around the web (my current project requires me to spend quite a lot of time looking at the JS used by other sites), I've been shocked by how many large and reputable sites fail to even use a minifier on their code. Apart from the potential for code theft if you care about that sort of thing, this is foing a great disservice to the site's users: on the web, larger code leads directly to longer load times. Minifying your JS is one of the quickest and cheapest ways to give your users a better experience on your site.

Closure Compiler is more than a minifier, though. Amongst its many tricks it also introduces a type system to Javascript. Actually it's more than a type system: it also allows you to declare the visibility of object properties, for example. Javascript typing in Closure is achieved using JsDoc comments, which means the actual Javascript syntax is unaltered and can still be run by any Javascript interpreter. Here's an example of a JsDoc type annotation:

/** @type {boolean} */
var isBroken = true;

Now, if you try to assign a number to isBroken, the compiler will warn you. Functions become much easier to read, too:

/**
 * Check whether a number is prime.
 * @param {number} myNum  The number to check for primeness.
 * @return {boolean} Whether the number is prime.
 */
function isPrime(mynum) {
  ...
}

And of course, if you try to pass a string into isPrime, the compiler will alert you to that, too.

Closure Compiler turns Javascript into a real programming language, and I love it for that. The great news is that it's completely free for anyone to use, so if you're developing a complex website or web application, I'd suggest giving it a go today.


*My aim here isn't to start a language war. If you think JS sucks, fair enough... there are good reasons to dislike it. Closure Compiler still makes is better.


Such is the article Compile your Javascript

That's an article Compile your Javascript this time, hopefully can benefit for you all. okay, see you in other article posting.

You are now reading the article Compile your Javascript with the link address https://wcest.blogspot.com/2013/08/compile-your-javascript.html