Created
October 22, 2012 18:25
-
-
Save ynonp/3933195 to your computer and use it in GitHub Desktop.
JS Prototypes
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
(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