DEV Community

Discussion on: Different types of Functions in JavaScript

Collapse
 
alvarocaceresmu profile image
Álvaro Cáceres Muñoz

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

Collapse
 
jaamaalxyz profile image
Md. Jamal Uddin

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!