Hey guys i have been trying to learn what the bind() method does in JS and i have found a few helpful resources in SO , MDN and also git that do explain this rather well , but i am still a bit confused with a practical example that i found on MDN. The below code i am talking about :
function LateBloomer() {
this.petalCount = Math.ceil(Math.random() * 12) + 1;
}
// Declare bloom after a delay of 1 second
LateBloomer.prototype.bloom = function() {
window.setTimeout(this.declare.bind(this), 1000);
};
LateBloomer.prototype.declare = function() {
console.log('I am a beautiful flower with ' +
this.petalCount + ' petals!');
};
now bind function just like call() or apply() , thats what i understood so far , but what it does is , it can delay the execution of a function while preserving or rather binding the value of the this
to a specific function .
now in the below lines of code :
LateBloomer.prototype.bloom = function() {
window.setTimeout(this.declare.bind(this), 1000);
};
what is the 1st this
pointing to ? and what is the secound this
pointing to ?