const len = nodeList.length for (let i = 0; i < len; i++) { nodeList[i].addEventListener('click', e => { console.log(this) e.target.style.color = colorList[i] }) }
ES6 for of array.entries()
1 2 3 4 5 6
consthandleEvent = color => e => e.target.style.color = color const iterator = nodeList.entries();
for (let [i, node] of iterator) { node.addEventListener('click', handleEvent(colorList[i])) }
1 2 3 4 5 6
Function.prototype.bind = function (context) { var that = this; returnfunction () { that.apply(context); } }
1 2 3 4 5 6
Function.prototype.bind = Function.prototype.bind || function(context) { var that = this; returnfunction() { return that.apply(context, arguments); } }
1 2 3 4 5 6 7 8 9
Function.prototype.bind = function (context) { var that = this; var args = Array.prototype.slice.call(arguments, 1);
Function.prototype.bind = function (context) { var that = this; var args = Array.prototype.slice.call(arguments, 1);
var fbound = function () { var bindArgs = Array.prototype.slice.call(arguments); that.apply(thisinstanceof that ? this : context, args.concat(bindArgs)); } fbound.prototype = this.prototype; return fbound; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
Function.prototype.bind = Function.prototype.bind || function (context, ...formerArgs) { let that = this;
if (typeofthis !== 'function') { thrownewTypeError("NOT_A_FUNCTION -- this is not callable"); } let fNOP = function () {};
let fbound = function (...laterArgs) { that.apply(thisinstanceof that ? this : context, formerArgs.concat(laterArgs)); };