JavaScript this binding
Posted on 2014-12-30
While reading articles this morning, I came across this one talking about anti-patterns.
Most of the things were small, but I learned about the thisArg and the bind function. They both prevent the use of var _this = this in JavaScript anonymous functions.
items.forEach(function (item) {
this.doSomething(item);
}, this)Here is a sample for the bind function.
el.click(function (e) {
this.clickCount++;
}.bind(this));Both are awesome for keeping code clean and simple.
comments powered by Disqus