Skip to content

Instantly share code, notes, and snippets.

@bterlson bterlson/gist:1fe0b0dc0ef3e71ff6e3 Secret
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.