Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created May 10, 2012 16:21
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rwaldron/2654256 to your computer and use it in GitHub Desktop.
Save rwaldron/2654256 to your computer and use it in GitHub Desktop.
Fat Arrows and Classes-as-Prototype-sugar make a beautiful future for JavaScript
var stream = require("fake-stream-lib"),
Emitter = require("events").EventEmitter,
util = require("util");
// Today...
function Device( opts ) {
this.value = null;
// open an async stream,
// this will be called continuously
stream.read( opts.path, function( data ) {
// Update this instance's current value
// with the most recent value from the
// data stream
this.value = data;
// bind(this) is required for correct scope
}.bind(this) );
// Throttle the frequency of events emitted from
// this Device instance
setInterval(function() {
// Emit a throttled event
this.emit("event");
// bind(this) is required for correct scope
}.bind(this), opts.freq || 100 );
}
util.inherits( Device, Emitter );
Device.prototype.scale = function( low, high ) {
return this.value * ( high - low ) / 1023 + low;
};
// ------------------------------------------------------ //
// w/ Fat Arrow...
function Device( opts ) {
this.value = null;
// open an async stream,
// this will be called continuously
stream.read( opts.path, ( data ) => {
// Update this instance's current value
// with the most recent value from the
// data stream
this.value = data;
// bind(this) is no longer required
});
// Throttle the frequency of events emitted from
// this Device instance
setInterval(() => {
// Emit a throttled event
this.emit("event");
// bind(this) is no longer required
}, opts.freq || 100 );
}
util.inherits( Device, Emitter );
Device.prototype.scale = function( low, high ) {
return this.value * ( high - low ) / 1023 + low;
};
// ------------------------------------------------------ //
// w/ Fat Arrow & Class...
class Device extends Emitter {
constructor( opts ) {
super();
this.value = null;
// open an async stream,
// this will be called continuously
// This example uses the expression
// form of a fat arrow
stream.read( opts.path, data => this.value = data );
// Throttle the frequency of events emitted from
// this Device instance
setInterval(() => {
// Emit a throttled event
this.emit("event");
}, opts.freq || 100 );
}
scale( low, high ) {
return this.value * ( high - low ) / 1023 + low;
}
}
@angus-c
Copy link

angus-c commented May 13, 2012 via email

@juandopazo
Copy link

And English keyboard require shift for " which I'm sure it's written more often than function or =.

Non-English keyboard usually suck for coding anyway. Spanish-Spain keyboard requires Alt-Gr for both types of brackets. Alt-Gr! Fortunately there's the Latinamerican keyboard that looks a little more like the English one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment