بتل کد Battle Codeدارد باز می‌شود…

The Short Constructor چالش 4 از جلسه‌ی 22، متوسط

این کلاس درست کار می‌کند، ولی بلند است و هر دو فیلدش از بیرون باز است.
آن را با سازنده‌ی کوتاه دوباره بنویس:
- name باید private باشد.
- level باید public readonly باشد.
- دو خطِ فیلد و دو خطِ this. ... = ... دیگر لازم نیستند.
برنامه باید همان را بنویسد: Sara (level 3) و در خط بعد 3

<style>
body { font-family: sans-serif; text-align: center; padding: 16px; }
button, input, select { font-size: 16px; min-height: 40px; margin: 4px; }
#out { direction: ltr; text-align: left; font: 16px monospace; margin: 12px 0; padding: 12px; min-height: 28px; border-radius: 12px; background: #0f172a; color: #e2e8f0; white-space: pre-wrap; overflow-wrap: anywhere; }
#out .line { min-height: 22px; }
</style>

<pre id="out"></pre>

<script type="text/typescript">
class Hero {
  name: string;
  level: number;

  constructor(name: string, level: number) {
    this.name = name;
    this.level = level;
  }

  describe(): string {
    return this.name + " (level " + this.level + ")";
  }
}

const hero = new Hero("Sara", 3);
show(hero.describe());
show(hero.level);
</script>

راهنما و جواب این چالش · برگشت به درس جلسه‌ی 22: درهای قفل · چالش بعدی: Find the Red Lines

بیشتر بخوان: جاوااسکریپت چیست؟ از دکمه‌ی وب تا Node.js