-
-
Save rwaldron/1084023 to your computer and use it in GitHub Desktop.
Array Extender. Creates an Array subclass. Doing so you won't override or mess with Array's own prototype
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 ArrayExtender = function (a) { | |
var counter = 0; | |
for (var i in a) { | |
if (a.hasOwnProperty(i)) { | |
this[i] = a[i]; | |
++counter; | |
} | |
} | |
this.length = counter; | |
}; | |
ArrayExtender.prototype = Array.prototype; | |
ArrayExtender.prototype.count = function () { | |
for(var i in this) { | |
if (this.hasOwnProperty(i)) { | |
console.log("Element "+i+" is "+this[i]); | |
} | |
} | |
}; | |
var thisWouldWin = new ArrayExtender([123, 234]); | |
console.log(thisWouldWin); | |
thisWouldWin.count(); | |
console.log(thisWouldWin.length); | |
thisWouldWin.push(345); | |
console.log(thisWouldWin.length); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment