1

I understand that class constructor properties will be assigned to the instance of the newly created object like so, but what is the different between the 2 code both of them will add isAdmin to the object

class User {
    constructor(name) {
        this.isAdmin = false
        this.name = name
    }
}

now with a class field

class User {
    isAdmin = true
    constructor(name) {
        this.name = name
    }
}

object will have both properties either way, so what is the difference?

const user = new User('john')

console.log(user)
{ isAdmin: true, name: "john" }
Taste of Leaving
  • 304
  • 2
  • 20

0 Answers0