Skip to content

Instantly share code, notes, and snippets.

@nzakas
Created September 12, 2011 19:24
Show Gist options
  • Save nzakas/1212121 to your computer and use it in GitHub Desktop.
Save nzakas/1212121 to your computer and use it in GitHub Desktop.
Stack implementation using ES6 proxies
/*
* Another ES6 Proxy experiment. This one creates a stack whose underlying
* implementation is an array. The proxy is used to filter out everything
* but "push", "pop", and "length" from the interface, making it a pure
* stack where you can't manipulate the contents.
*/
var Stack = (function(){
var stack = [],
allowed = [ "push", "pop", "length" ];
return Proxy.create({
get: function(receiver, name){;
if (allowed.indexOf(name) > -1){
if(typeof stack[name] == "function"){
return stack[name].bind(stack);
} else {
return stack[name];
}
} else {
return undefined;
}
}
});
});
var mystack = new Stack();
mystack.push("hi");
mystack.push("goodbye");
console.log(mystack.length); //1
console.log(mystack[0]); //undefined
console.log(mystack.pop()); //"goodbye"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment