How to obtain harmony in your node.js
UPDATE! 3rdEden has pointed out that it’s possible to get harmony in node 0.6
You can get it by using a flag when invoking node through the command line
node --harmony_typeof --harmony_block_scoping --harmony_proxies --harmony_weakmaps file.js
Or if you’re using node 0.7.x
node --harmony file.js
Harmony!!!!1
Enable ECMAScript’s new features and syntax in node point javascript.
Node unstable has just been released and with that V8 has been upgraded to 3.8.6 which contains flags for experimental language features.
By default those flags are turned off, but you can always turn them on. Here’s how:
Download node 0.7.0.
If you’ve got it cloned on git run a git fetch and then git checkout v0.7.0 otherwise read the release notes for instructions on where to download.
Set harmony flag to true in V8
Once you’ve got the source code, open up deps/v8/src/flag-definitions.h and look for Line 115
Change the flag from false to true.
DEFINE_bool(harmony, true, "enable all harmony features")
Compile Node
./configure && make && make install
Have fun!
function foo() {
"use strict";
let i = -1;
for (let i = 0; i < 10; i += 1) {
console.log(i); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
}
console.log(i); // -1
}
foo();
NOTE: you’ll need to use "use strict"; otherwise the code above will not run.
By doing this you’ll get:
- Sets, Maps, and Weak Maps
- Block scoping
- Proxies
- New typeof semantics
Learn more about ES harmony
On case against coffeescript...
There are basically a few arguments against using CoffeeScript and at the end of the article Ryan Florence suggests one to not use it. It basically boils down to these cases:
- Debugging is an issue
- Verbally Readable !== Quicker Comprehension
- One liners are horrible
- CoffeeScript has it’s bad parts
- Significant whitespace + Spaghetti code is bad
- CoffeeScript will never be supported natively.
Debugging is an issue
So yes, debugging is an issue. Hopefully this can be solved soon by SourceMap.
Verbally Readable does not mean Quicker Comprehension.
I agree, in fact I comprehend Verbally Readable !== Quicker Comprehension quicker than I do the title. But the nice thing about CoffeeScript is that it’s just JavaScript so the lengthy wordy operators are optional.
You could always write in CoffeeScript:
one && two && three
instead of
one and two and three
One liners are horrible
Here’s an example from the blog:
scores = (student["assignment_#{@assignment.id}"].score for own idx, student of @gradebook.students when student["assignment_#{@assignment.id}"]?.score?)
Right, that’s not easy to comprehend. In this particular case I don’t blame the language but the author.
Sure the CoffeeScript documentation touts how it turns verbose JavaScript into “nice” and “sexy” one-liners but that doesn’t mean one should abuse it.
CoffeeScript has it’s bad parts
So does every other language. I think the good parts of the language outweigh the bad parts.
Significant whitespace + spaghetti code is bad
Spaghetti code itself is bad. I don’t care what language you write it in. It’ll always look like shit.
It seems like now we’re talking about preferring one type of spaghetti over another type of spaghetti — it’s still spaghetti.
CoffeeScript will never be supported natively
Is that really a bad thing?
Overall
I still believe the expressiveness and useful features that CoffeeScript provides outweighs the biggest problem which is debugging.
Introducing gister
A nodejs module for programmatic access to create, edit and retrieve gists from github.
There are quite a few gist libraries available from npm. Most were CLI apps to create new gists, others didn’t exactly meet my needs. So I set out to create gister which provides what I think is a simple API. It follows the observer pattern and uses request to talk to GitHub.
Read the annotated source or check out the code.
Using it is really simple. Here’s how you retrieve a gist with gist_id = 1:
var gist = new Gist({ gist_id: 1 });
gist.on('get', function (data) {
// do something with data
});
gist.get();
Creating a new gist is similar, although you’ll need to provide Gist with your GitHub username and Secret API Token which can be found in your Account Settings
var gist = new Gist({ username: 'goatslacker', token: 'abc123' });
gist.on('created', function (data, gist_id) {
// gist_id is the newly created gist id
// data is what github returns in it's reply
});
gist.create();
Available on npm. Install:
npm install gister