Call and Apply

Call and apply is that call requires the arguments to be passed in one-by-one, and apply takes the arguments as an array.

A for array and C for comma.”

function longerSummary(genre, year) {
  console.log(
    `${this.title} was written by ${this.author}. It is a ${genre} novel written in ${year}.`
  )
}
longerSummary.call(book, 'dystopian', 1932)
longerSummary.apply(book, ['dystopian', 1932])
Functions vs arrow

Arrow functions do not have their own this binding. Instead, they go up to the next level of execution.

const whoAmI = {
  name: 'Leslie Knope',
  regularFunction: function () {
    console.log(this.name)
  },
  arrowFunction: () => {
    console.log(this.name)
  },
}

whoAmI.regularFunction() // "Leslie Knope"
whoAmI.arrowFunction() // undefined


https://www.taniarascia.com/this-bind-call-apply-javascript/

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *