Created
November 25, 2013 12:27
-
-
Save variousauthors/7640545 to your computer and use it in GitHub Desktop.
This is a stack implemented without assignment statements.
This file contains hidden or 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 Stack = function () { | |
return { | |
pop: function() { | |
throw "popped from an empty stack"; | |
}, | |
push: function(value) { | |
return (function (self) { | |
return _.extend({}, self._incrementCount(), { | |
head: value, | |
pop: function () { | |
return self; | |
} | |
}); | |
})(this); | |
}, | |
_incrementCount: function () { | |
return _.extend({}, this, { count: this._getCount() + 1 }); | |
}, | |
_getCount: function () { | |
if (this.count) { | |
return this.count; | |
} else { | |
return 0; | |
} | |
}, | |
peek: function () { | |
return this.head; | |
} | |
}; | |
}; | |
var bob = new Stack; | |
var jane = bob.push(3).push(4).pop(); | |
console.log(jane.peek()); | |
console.log(jane.peek()); |
WHO IS JANE AND WHAT IS SHE DOING WITH BOB????????
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a fiddle to play with.