Skip to content

Instantly share code, notes, and snippets.

@bterlson
Created September 24, 2014 20:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bterlson/95ab741efa0bbd57f19d to your computer and use it in GitHub Desktop.
Save bterlson/95ab741efa0bbd57f19d to your computer and use it in GitHub Desktop.
var create = Symbol();
function protocolize(C) {
return new Proxy(C, {
construct: function(target, args) {
let createfn = target[create];
if(createfn) {
$this = createfn(...args);
$ret = target.apply($this, args);
if($ret !== null && typeof $ret === 'object') return $ret;
else return $this;
} else {
return Reflect.construct(target, args);
}
}
})
}
let Stack = protocolize(class extends Array {
constructor(...args) {
super(...args);
}
static [create]() {
return [];
}
});
let c = new Stack();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment