Skip to content

Instantly share code, notes, and snippets.

@bterlson
Last active August 29, 2015 14:06
Show Gist options
  • Save bterlson/1fe0b0dc0ef3e71ff6e3 to your computer and use it in GitHub Desktop.
Save bterlson/1fe0b0dc0ef3e71ff6e3 to your computer and use it in GitHub Desktop.
let create = Symbol();
Array[create] = function(args) {
return new Array(...args)
}
function protocolize(C) {
return new Proxy(C, {
construct: function(target, args) {
let createfn = target.hasOwnProperty(create) ? target[create] : undefined;
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);
}
});
let c = new Stack();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment