Skip to content

Instantly share code, notes, and snippets.

@dagolinuxoid
Created March 8, 2018 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dagolinuxoid/02953303c36870c62030c11bf44c436e to your computer and use it in GitHub Desktop.
Save dagolinuxoid/02953303c36870c62030c11bf44c436e to your computer and use it in GitHub Desktop.
bicycles | proto | examples | eazy-breezy
//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