Some notes on converting CoffeeScript to ECMAScript2015 (ES6) for voxel.js-related projects
Add to beginning of file 'use strict';
Add variable declarations - usually const
, but sometimes let
More scoping clarity (explicit) with ES6 than CoffeeScript - other criticism of CoffeeScript scoping:
CoffeeScript's Scoping is Madness
Add semicolons - although arguably, standard says not, due to semicolon insertion, but I add them anyways
Add parenthesizes around function calls
CS | ES6 |
---|---|
Inventory = require 'inventory'; |
const Inventory = require('inventory'); |
Replace YAML-style literals with JSON-style literals - mostly add braces, commas if needed
module.exports.pluginInfo =
'loadAfter': ['voxel-recipes', 'voxel-carry', 'voxel-registry']
becomes:
module.exports.pluginInfo = {
'loadAfter': ['voxel-recipes', 'voxel-carry', 'voxel-registry']
};
Arrow functions - some similaries (though be mindful of this
):
CS | ES6 |
---|---|
module.exports = (game, opts) -> new InventoryCrafting(game, opts) |
module.exports = (game, opts) => new InventoryCrafting(game, opts); |
Template strings - #{...}
becomes ${...}
, and change double-quotes to backquotes
Classes - same class
and extend
keyword, but add brace after
Method syntax is cleaner:
CS | ES6 |
---|---|
updateCraftingRecipe: () -> |
updateCraftingRecipe() { |
Instance variables - replace @
with this.
, except for bare @
(legal in older CoffeeScript) which becomes this
Superconstructor - same super
call, but annoyingly, may need to reorganize it to be called before assigning this.
! Minor refactoring of constructors.