Skip to content

Instantly share code, notes, and snippets.

@akinjide
Created February 27, 2018 18:05
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 akinjide/3a3d9ff35458e026ca95d1a2e27a8de5 to your computer and use it in GitHub Desktop.
Save akinjide/3a3d9ff35458e026ca95d1a2e27a8de5 to your computer and use it in GitHub Desktop.
Callback
Array.prototype.click = function(fn, thisArg) {
const _self = thisArg || this;
for (let i = 0; i < _self.length; i++) {
fn.apply(thisArg, [_self[i], i, thisArg]);
// Mutation.
thisArg.values[i] = i;
}
};
let values = ['Hello', 'World'];
values.click(function(value, index, ctx) {
console.log(value, index);
}, this);
console.log(values, this.values);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment