DEV Community

Discussion on: Javascript call and apply 101

Collapse
 
iggredible profile image
Igor Irianto • Edited

Hey Jannik! I'm glad you liked it.
As for the usage, it is mostly up to your preference. In addition to what Jake covered (for arguments), you can use call and apply to give this inside a function a different object.

const someObj = {
  name: 'iggy',
  sayName() {
     console.log(`The name is ${this.name}`)
  }
}

Normally I'd do someObj.sayName(). But if one day I go back to reuse sayName, but I want to pass it a different name, you can just use call/apply. someObj.sayName.call({name: 'Russell'}).

It's like inheriting and reusing other object's properties.