JSFiddle - React, Tailwind, and code Playground
by tonyleeper
JavaScript
/**
.bind() with jQuery.each()
demonstrates how you can pass the context to the anonymous function.
*/
// passing context using .bind()
var O = function () {
this.data = ["foo"];
};
O.prototype.test = function () {
$(this.data).each(function (i, element) {
console.log(element);
console.log(this.data.length);
}.bind(this));
};
var x = new O();
x.test();
// alternate method using a closure
var OO = function () {
this.data = ["foo"];
};
OO.prototype.test = function () {
var self = this;
$.each(this.data, function (i, element) {
console.log(element);
console.log(self.data.length);
});
};
var y = new OO();
y.test();