EasyPush
EasyPush

Reputation: 776

How do i call a function of an object as if it was the property of another object?

Now this may be an odd question, but consider the following:

var A = {
  name: "A",
  showName: function(){console.log(this.name)}
}

var B = {
  name: "B"
}

I now want to call the "showName" function of A as if it was a property of B, meaning it should log "B" to the console. Is there any reasonable way to accomplish this?

Upvotes: 2

Views: 43

Answers (1)

Denys Séguret
Denys Séguret

Reputation: 382150

Simply use call :

A.showName.call(B);

Upvotes: 4

Related Questions