Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created May 17, 2012 17:04
Show Gist options
  • Save rwaldron/2720228 to your computer and use it in GitHub Desktop.
Save rwaldron/2720228 to your computer and use it in GitHub Desktop.
Traceur Experiments: class constructor
// For context, See original: https://gist.github.com/2654256
class Device {
constructor( 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 );
}
scale( low, high ) {
return this.value * ( high - low ) / 1023 + low;
}
emit( event, data ) {
console.log( event, data );
}
}
// Output:
var Device = traceur.runtime.createClass("Device", null, null, null, undefined, {
constructor: function(opts) {
this.value = null;
setInterval(function() {
this.emit("event");
}, opts.freq || 100);
},
scale: function(low, high) {
return this.value *(high - low) / 1023 + low;
},
emit: function(event, data) {
console.log(event, data);
}
}, function $static() { }, null);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment