top | item 8815797

(no title)

wpears | 11 years ago

His second bind example is incorrect.

  function handleClick(i) {  
    this.innerHTML = i;
  }

  for (i = 0; i < elems.length; i++) {  
    elems[i].addEventListener("click", handleClick.bind(this, i));
  }
In the for loop, the this value refers to the global object. To get the desired effect, it should be changed to elems[i] (this piece of code is trying to bind a handler to an element).

discuss

order

No comments yet.