Skip to main content

ECMAScript 6

Today is a rather significant one for me as JavaScript/Rails programmer because Sprockets (https://github.com/rails/sprockets) upgraded to version 3 meaning I can now use the Sprockets-es6 gem (https://github.com/josh/sprockets-es6) to transpile ES6 JavaScript to ES5.

Now to those that didn't understand the last paragraph, here's what it means...

The language we commonly call JavaScript is actually formally known as ECMAScript (http://en.wikipedia.org/wiki/ECMAScript). Since 2009 all the major browsers have been interpreting ECMAScript version 5. In fact they mostly still do.

Version 6 is due to be formalised any day now (well, June 2015) which means that a whole new slew of functionality is due to be added to the language (class constructors, formal inheritance, fat arrows, etc...). Browser makers will start implementing these new features once the spec is complete.

All of this sounds great, but for 2 things

1) I want to start using ES6 today
2) Even if the newly released browsers did come out today, people are not all going to upgrade their browsers as soon as the new ones come out. Typically there is about a year lag before a new feature (or set of features) has enough critical mass to be used with any confidence.

So what can we do?

Well thankfully there are transpilers which can take most of the features of ES6, and rewrite them as ES5. The 2 main ones are Babel (https://babeljs.io/) and Traceur (https://github.com/google/traceur-compiler). For various reasons, I chose to use Babel (because I found their resulting code more readable and because their gem, Sprockets-es6, seemed to have the easiest to add to our project flow). (Basically Traceur didn't work with Phantom which is what we use for testing and is actually running ES4!).

Unfortunately, sprockets-es6 had a dependency on sprockets 3 which was in beta up until this weekend.

And now it's out of beta!

As I write this I am just testing it out on our develop server and hopefully I will go live with it soon.

Happy days.

If you want an example of what can be done with ES6, you can mess around with it here.

http://www.es6fiddle.net/


Comments

Popular posts from this blog

Freezing Gems

What is a gem and why would you want to freeze it? In Ruby, there are times when you want to access pieces of functionality that other people of written (3rd party libraries) and you normally have 2 options. You can install a plug in or install a gem. Normally the method you use is determined by which ever is made available by the author. Gems are installed on the host machine and are pretty handy when you want to run things in the command line or else across lots of projects, but their downside is that if you use a gem in a Rails project there is no automatic publishing mechanism when you deploy your site. You will need to log onto the remote host machine and install the gem manually. Plugins are specific to Rails and are similar to gems in that they are also 3rd party libraries. However they are associated with your Rails project as opposed to your machine so they will get posted to the server on a regular deploy. Freezing a gem is the process of transforming a gem into a plug in. Es

Elixir - destructuring, function overloading and pattern matching

Why am I covering 3 Elixir topics at once? Well, perhaps it is to show you how the three are used together. Individually, any of these 3 are interesting, but combined, they provide you with a means of essentially getting rid of conditionals and spaghetti logic. Consider the following function. def greet_beatle(person) do case person.first_name do "John" -> "Hello John." "Paul" -> "Good day Paul." "George" -> "Georgie boy, how you doing?" "Ringo" -> "What a drummer!" _-> "You are not a Beatle, #{person.first_name}" end end Yeah, it basically works, but there is a big old case statement in there. If you wanted to do something more as well depending on the person, you could easily end up with some spaghetti logic. Let's see how we can simplify this a little. def greet_beatle(%{first_name: first_name}) do case first_name d

Responsive Web Design

I wanted to go over Responsive Web Design using CSS. In the old days of web development, we had to code to common screen sizes (i.e. 800 X 600, 1024 X 768) and we patiently waited for people to upgrade their computers to have a decent amount of screen real estate so we could design things the way we really wanted. We also took on semi stretchy web layouts etc to expand and contract appropriately. Then about 2 or 3 years ago, Apple released this little device called an iPhone with a 320 X 480 resolution which took the world by storm and suddenly a lot of people were viewing your website on a tiny screen again... Anyways, as it can be difficult to design a site which looks good on 320 X 480 AND 1680 X 1050, we need to come up with some kind of solution. One way is to sniff the client and then use an appropriate stylesheet, but then you are mixing CSS with either JavaScript or server side programming and also potentially maintaining a list of appropriate clients and stylesheets. Also, you