TGViewer
JavaScript JavaScript @javascript · 31.1K subscribers
Post #3156 2.22K
CHALLENGE

class Animal {
#sound;

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

speak() {
return `${this.name} says ${this.#sound}`;
}

static create(name, sound) {
return new this(name, sound);
}
}

class Dog extends Animal {
#tricks = [];

constructor(name) {
super(name, "woof");
}

learn(trick) {
this.#tricks.push(trick);
return this;
}

perform() {
return `${this.name} performs: ${this.#tricks.join(", ")}`;
}
}

const rex = Dog.create("Rex", "bark");
rex.learn("sit").learn("shake").learn("roll over");

console.log(rex.speak());
console.log(rex.perform());
console.log(rex instanceof Animal);
console.log(rex instanceof Dog);
More from @javascript
  1. Sep 25, 2026Post #3518
  2. Sep 25, 2026CHALLENGE let counter = 0; const mod = { get counter() { return counter; }, increment() {…
  3. Sep 24, 2026Post #3516
  4. Sep 24, 2026CHALLENGE class Base { static count = 0; static increment() { this.count++; return this.co…
  5. Sep 23, 2026🤩 tinyjs: Build JavaScript Desktop Apps in <10MB I built an app with this and was impress…
  6. Sep 23, 2026Post #3513
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →