Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created October 22, 2012 18:25
Show Gist options
  • Save ynonp/3933195 to your computer and use it in GitHub Desktop.
Save ynonp/3933195 to your computer and use it in GitHub Desktop.
JS Prototypes
(function () {
function object(parent) {

function F() {};
F.prototype = parent || Object;
return new F();
}
var Stack = function() {
this.data = [];
this.push = function(x) {
this.data.push(x);
};
this.pop = function() {
return this.data.pop();
};
};
var CountedStack = function() {
this.data = [];
this.count = 0;
this._super = {};
this._super.push = this.push;
this.push = function() {
this.count++;
this._super.push.apply( this, arguments );
};
this.countPushes = function() {
return this.count;
};
};
CountedStack.prototype = new Stack();
var c1 = new CountedStack();
var c2 = new CountedStack();
c1.push(7);
c2.push(10);
console.log(c1.pop());
console.log(c1.countPushes());
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment