-
-
Save bterlson/95ab741efa0bbd57f19d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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