0

One could write a greet method for instances of a Person object in at least two ways:

function Person(name) {
  this.name = name
  this.greet = () => {
    console.log(`hello, my name is ${this.name}`)
  }
}

// or
function Person(name) {
  this.name = name
}

Person.prototype.greet = function() {
  console.log(`hello, my name is ${this.name}`)
}

It's common best practice to extend an objects prototype rather than define methods within the constructor.

Is there ever a use case for defining instance methods in a constructor function?

Andrew Kim
  • 3,145
  • 4
  • 22
  • 42

0 Answers0