re: Different type of functions in JavaScript VIEW POST

VIEW FULL DISCUSSION

Nice article! You managed to make me understand generators for the first time XD.

By the way, could you please give me a practical use case of the Function consctructor? Thank you

here is an example Function constructor with implementing a class and you know JavaScript class is a syntactic sugar over the function structure.


class Animal {
  constructor(name, energy) {
    this.name = name
    this.energy = energy
  }
  eat(amount) {
    console.log(`${this.name} is eating.`)
    this.energy += amount
  }
  sleep() {
    console.log(`${this.name} is sleeping.`)
    this.energy += length
  }
  play() {
    console.log(`${this.name} is playing.`)
    this.energy -= length
  }
}

const leo = new Animal('Leo', 7)

wanna know more, please check out this stackoverflow thread

Thanks!

code of conduct - report abuse