Last active
August 29, 2015 14:10
-
-
Save zhoufenfens/b269f46110207fcab701 to your computer and use it in GitHub Desktop.
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 SuppedArray() {} | |
SuppedArray.prototype = new Array; | |
SuppedArray.prototype.forEach = function (fun) { | |
var len = this.length; | |
while (len--) { | |
fun(this[len]); | |
} | |
}; | |
function SuperPowerArray(len) { | |
this.usedLength = 0; | |
} | |
SuperPowerArray.prototype = new Uint8Array; | |
SuperPowerArray.prototype.forEach = function (fun) { | |
var i = this.length; | |
while (i--) { | |
fun(this[i]); | |
} | |
}; | |
SuperPowerArray.prototype.push = function (val) { | |
this[this.usedLength] = val; | |
this.usedLength += 1; | |
}; | |
function PowerArray() { | |
var array = []; | |
return { | |
push: array.push, | |
forEach: function (fun) { | |
var i = array.length; | |
while (i--) { | |
fun(array[i], i); | |
} | |
}, | |
map: function (fun) { | |
var results = PowerArray(); | |
array.forEach(function (elem) { | |
results.push(fun(elem)); | |
}); | |
return results; | |
}, | |
at: function (i) { | |
return array[i]; | |
} | |
}; | |
} | |
module.exports = { | |
SuppedArray: SuppedArray, | |
SuperPowerArray: SuperPowerArray, | |
PowerArray: PowerArray | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment