Private field must be declared in an enclosing class in JS-Code Examples

Radu TM

Private field must be declared in an enclosing class in JS

JavaScript by Radu TM June 21, 2022

class Emoji {
  constructor(name, meaning) {
    // name and meaning are private fields
    this.#name = name;
    this.#meaning = meaning;
  }

  toString() {
    return `${this.#name} means ${this.#meaning}`;
  }
}

const emoji = new Emoji('🍕', 'pizza');
console.log(emoji.toString()); // 🍕 means pizza

0

41.897

Private field must be declared in an enclosing class in JS

JavaScript by Radu TM June 21, 2022

class Person {
  #name;

  constructor(name) {
    this.#name = name;
  }

  get name() {
    return this.#name;
  }
}

const person = new Person('🦄');
console.log(person.name); // '🦄'

0

41.897

Related JavaScript Queries

Latest JavaScript Queries

Explore code examples by categories