3️⃣

6.3 Difference Between Arrow and Regular Method

6.3 Difference Between Arrow and Regular Method

This is going off the self-referencing that was mentioned in the previous chapter.

When using the this keyword, using an arrow function always refer to the outer scope, while the regular function method refers to the local scope.

Example

const car = {
  brand: 'Ford',
  model: 'Fiesta',
  start: function() {
    console.log(`Started ${this.brand} ${this.model}`)
  },
  stop: () => {
    console.log(`Stopped ${this.brand} ${this.model}`)
  }
}


car.start(); // Started Ford Fiesta
car.stop(); // Stopped undefined undefined

Here the arrow function was unable to get access to the brand and model field and was forced to use undefined.

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.