bind with settimeout
by sramnan
JavaScript
function Person(name){
this.name = name;
this.distractedGreeting = function() {
var self = this; // <-- this line!
setTimeout(function(){
console.log("Hello, my name is " + self.name); // <-- and here!
}, 500);
}
}
function Person1(name){
this.name = name;
this.distractedGreeting = function() {
var self = this; // <-- this line!
setTimeout(function(){
console.log("Hello, my name is " + this.name); // <-- and here!
}.bind(this), 500);
}
}
var person = new Person("sonia");
person.distractedGreeting();
var person1 = new Person("soni1a");
person1.distractedGreeting();