Created
March 8, 2018 11:24
-
-
Save dagolinuxoid/02953303c36870c62030c11bf44c436e to your computer and use it in GitHub Desktop.
bicycles | proto | examples | eazy-breezy
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
//polyfil map == transformer | |
Array.prototype.transformer = function (callback) { | |
const transformedArr = []; | |
for (let i=0; i<this.length; i++) { | |
transformedArr.push(callback(this[i],i,this)); | |
} | |
return transformedArr; | |
}; | |
var numbers = [1,2,3,4,5]; | |
const doubleNums = number => number * 2; | |
var doubles = numbers.transformer(doubleNums); | |
console.log(doubles); | |
// reduce polyfil (also simplified | no checking against initValue and restricted arg list) | |
var treasure = { | |
simplify: function(callback, initValue) { | |
let accum = initValue; | |
for(let i=0; i<this.length; i++) { | |
accum = callback(accum, this[i]); | |
} | |
return accum; | |
} | |
}; | |
var list = [1,2,3,4]; | |
//list.__proto__ = treasure; | |
Object.setPrototypeOf(list,treasure); | |
console.log( list.simplify((acc,n)=>acc+n, 0 )); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment