I have the following code below which is an object literal with two functions and one property.
My problem is that when function greetSomeone
gets called, it's context is not person, but an event (eg jQuery or DOM event), therefore the context changes to the event that was fired and this.getName
is not found.
So, how do I call this.getName()
inside greetSomeone
to execute correctly?
I tried getName.call(person)
but it did not work.
var person = {
name: 'john',
greetSomeone: function(event) {
//i tried getName.call(person) but doesn't work
return 'hello i am ' + this.getName() //<---this doesn't work
},
getName: function() {
return this.name
}
};
console.log(person.greetSomeone()) // works here, but not in an event handler