-
-
Save juandopazo/2654352 to your computer and use it in GitHub Desktop.
Fat Arrows and Classes-as-Prototype-sugar make a beautiful future for JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ); | |
// Throttle the frequency of events emitted from | |
// this Device instance | |
setInterval(function() { | |
// Emit a throttled event | |
this.emit("event"); | |
}.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; | |
}); | |
// Throttle the frequency of events emitted from | |
// this Device instance | |
setInterval(() => { | |
// Emit a throttled event | |
this.emit("event"); | |
}, 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; | |
// This example uses the expression | |
// form of a fat arrow | |
stream.read( 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; | |
} | |
} | |
// ------------------------------------------------------ // | |
// w/ Fat Arrow, Class & Destructuring with default parameter values... | |
class Device extends Emitter { | |
constructor({ path, freq = 100 }) { | |
super(); | |
this.value = null; | |
// This example uses the expression | |
// form of a fat arrow | |
stream.read( path, data => this.value = data ); | |
// Throttle the frequency of events emitted from | |
// this Device instance | |
setInterval(() => { | |
// Emit a throttled event | |
this.emit("event"); | |
}, freq ); | |
} | |
scale( low, high ) { | |
return this.value * ( high - low ) / 1023 + low; | |
} | |
} |
The only part that isn't covered in the ES wiki is how destructuring of an object parameter works with default values :(
I just asked @allenwb and updated the gist. It allows the use of = in the object parameter.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
VERY COOL!