Created
July 15, 2011 03:33
-
-
Save leobalter/1084009 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; | |
}; | |
var ArrProto = Array.prototype; | |
var ArrExtProto = {}; | |
for (var i in ArrProto) { | |
if (ArrProto.hasOwnProperty(i)) { | |
ArrExtProto[i] = ArrProto[i]; | |
} | |
} | |
ArrayExtender.prototype = ArrExtProto; | |
ArrayExtender.prototype.count = function () { | |
for(var i in this) { | |
if (this.hasOwnProperty(i)) { | |
console.log("Element "+i+" is "+this[i]); | |
} | |
} | |
}; | |
console.log("2 lines returning an expression and a undefined value"); | |
console.log(ArrayExtender.prototype.count); | |
console.log(Array.prototype.count); | |
console.log("now this should return more lines"); | |
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
this is happening in Opera's dragonfly too...
if someone else didn't get the trick check the console at line 39's return: [123, 234, 345], but 345 is pushed after this.