Skip to content

Instantly share code, notes, and snippets.

@trevnorris
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save trevnorris/e3144f66dfa0aac6ff27 to your computer and use it in GitHub Desktop.

Select an option

Save trevnorris/e3144f66dfa0aac6ff27 to your computer and use it in GitHub Desktop.
'use strict';
class Readable {
constructor() {
this._onreadable = null;
// Acquire the default onreadable callback if it's been set.
if (typeof this.constructor._onreadable === 'function')
this._onreadable = this.constructor._onreadable;
}
static onreadable(cb) {
if (typeof cb !== 'function')
throw new TypeError('callback must be a function');
this._onreadable = cb;
}
}
Readable._onreadable = null;
// Extending this
class MyReadable extends Readable {
constructor() {
super();
}
}
// Don't want the internal Object Map to change, so still have to do this.
MyReadable._onreadable = undefined;
class MyReadable2 extends Readable {
constructor() {
super();
}
}
// Don't want the internal Object Map to change, so still have to do this?
MyReadable2._onreadable = undefined;
print(Object.keys(MyReadable));
Readable.onreadable(function foo() { });
MyReadable.onreadable(function bar() { });
MyReadable2.onreadable(function baz() { });
let r = new Readable();
//print(Readable._onreadable); // output: function foo() { }
//print(r._onreadable); // output: function foo() { }
let mr = new MyReadable();
//print(MyReadable._onreadable); // output: function bar() { }
//print(mr._onreadable); // output: function bar() { }
let mr2 = new MyReadable2();
//print(MyReadable2._onreadable); // output: function baz() { }
//print(mr2._onreadable); // output: function baz() { }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment