'Difference between passing JQuery.append.bind vs anonymous function to a Array.forEach loop
I get two different behaviors out of these, I was hoping someone could explain why.
function Test( id ) {
this.target = $(id);
this.children = [ $('<div/>').text("A"), $('<div/>').text("B") ];
}
// will add 0 1 A B
Test.prototype.addToTarget = function() {
this.children.forEach(this.target.append.bind(this.target));
};
// will add A B
Test.prototype.addToTargetEx = function() {
var target = this.target;
this.children.forEach(function(child){
target.append(child);
});
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
