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

جلسه‌ی 12: اینترفیس Interfaces

با interface راه دوم اسم دادن به شکل یک شیء را یاد می‌گیری. بعد دو ابزار تازه می‌بینی: فیلدی که می‌تواند نباشد (?) و فیلدی که بعد از ساختن دیگر عوض نمی‌شود (readonly).

در این جلسه یاد می‌گیری

درس

در بازی علی بعضی بازیکن‌ها لقب دارند و بعضی نه. برنامه‌ی جاوااسکریپتش به همه می‌گوید Hi و بعد لقب. برای کسی که لقب ندارد چه می‌نویسد؟ Hi undefined! باز هم یک اشتباه بی‌صدا. یک اشتباه دیگر هم دارد: جایی از کد، شماره‌ی بازیکن (id) اشتباهی عوض شده و حالا دو بازیکن یک شماره دارند. امروز برای هر دو یک ابزار یاد می‌گیری.

اول یک راه تازه برای اسم دادن به شکل: interface. همان کار type جلسه‌ی قبل را می‌کند، با دو فرق کوچک در نوشتن: بعد از اسم = ندارد و هر فیلد را در یک خط می‌نویسیم. بقیه‌ی چیزها مثل قبل است: فیلد غلط، جاافتاده یا اضافه خط قرمز می‌شود.

<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">
interface Player {
  name: string;
  score: number;
}

const sara: Player = { name: "Sara", score: 10 };
show(sara.name, sara.score);
</script>

حالا لقب. اگر بنویسی nick: string، همه باید لقب داشته باشند. یک ? بعد از اسم فیلد بگذار: nick?: string. یعنی «این فیلد می‌تواند باشد، می‌تواند نباشد. ولی اگر بود، متن است». حالا هر دو شیء قبول می‌شوند.

<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">
interface Player {
  name: string;
  nick?: string;
}

const sara: Player = { name: "Sara", nick: "Sari" };
const ali: Player = { name: "Ali" };
show(sara.name, ali.name);
</script>

ولی یک فیلد اختیاری را نمی‌شود همین‌طوری به کار برد. این تابع لقب را با حروف بزرگ می‌نویسد. اجرا کن: خط قرمز! تایپ‌اسکریپت می‌گوید p.nick شاید خالی باشد (undefined، یعنی «چیزی این‌جا نیست»). و از «هیچ‌چیز» نمی‌شود toUpperCase خواست. در جاوااسکریپت همین خط برای علی برنامه را وسط کار می‌شکست.

<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">
interface Player {
  name: string;
  nick?: string;
}

function shout(p: Player): string {
  return p.nick.toUpperCase();
}

show(shout({ name: "Sara", nick: "Sari" }));
show(shout({ name: "Ali" }));
</script>

راه‌حل: اول بپرس. if (p.nick) { ... } یعنی «اگر لقبی هست». داخل این if تایپ‌اسکریپت مطمئن است که p.nick یک متن واقعی است و خط قرمز نمی‌دهد. بیرون از if هم برای کسی که لقب ندارد یک جواب دیگر برمی‌گردانی.

<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">
interface Player {
  name: string;
  nick?: string;
}

function shout(p: Player): string {
  if (p.nick) {
    return p.nick.toUpperCase();
  }
  return p.name.toUpperCase();
}

show(shout({ name: "Sara", nick: "Sari" }));
show(shout({ name: "Ali" }));
</script>

حالا شماره‌ی بازیکن. بعضی فیلدها بعد از ساخته شدن نباید عوض شوند. قبل از اسم فیلد بنویس readonly، یعنی «فقط خواندنی». موقع ساختن شیء مقدارش را می‌دهی و بعد از آن فقط می‌توانی آن را بخوانی. اجرا کن: خطی که id را عوض می‌کند قرمز است.

<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">
interface Player {
  readonly id: number;
  name: string;
}

const sara: Player = { id: 7, name: "Sara" };
sara.id = 8;
show(sara.id, sara.name);
</script>

readonly فقط همان یک فیلد را قفل می‌کند. بقیه‌ی فیلدها مثل قبل عوض می‌شوند. دو اشتباه رایج: ? قبل از : می‌آید (nick?: string، نه nick: string?). و readonly قبل از اسم فیلد می‌آید. اجرا کن: اسم عوض می‌شود، شماره همان می‌ماند.

<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">
interface Player {
  readonly id: number;
  name: string;
  nick?: string;
}

const p: Player = { id: 7, name: "Sara" };
p.name = "Sara Star";
p.nick = "Sari";
show(p.id, p.name, p.nick);
</script>

مثال‌های این جلسه

چالش‌ها

  1. Your First Interface (آسان)
  2. Ali Has No Nickname (آسان)
  3. Lock the Ticket Number (متوسط)
  4. Hi undefined (متوسط)
  5. Find the Red Lines (سخت)
  6. The Club Card (سخت)

بیشتر بخوان

← جلسه‌ی 11: نوعِ شیء جلسه‌ی 13: چیز در چیز →