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

جلسه‌ی 7: ورودیِ شاید Maybe an Input

یاد می‌گیری ورودی‌ای بسازی که دادنش اجباری نیست: با ? یا با یک مقدار پیش‌فرض. با void هم آشنا می‌شوی: نوع تابعی که کاری می‌کند ولی جوابی برنمی‌گرداند.

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

درس

جلسه‌ی قبل دیدی که تایپ‌اسکریپت ورودیِ جاافتاده را نمی‌بخشد. ولی گاهی واقعاً می‌خواهی یک ورودی اختیاری باشد. مثلاً تابع greet یک اسم می‌گیرد و گاهی یک لقب هم، مثل Dr. سارا لقب دارد، علی ندارد. اجرا کن: خط آخر قرمز است، چون تابع دو ورودی می‌خواهد و علی فقط یکی داده.

<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">
function greet(name: string, title: string): string {
  return title + " " + name;
}
show(greet("Sara", "Dr"));
show(greet("Ali"));
</script>

راهش یک علامت سؤال است. title?: string یعنی «title اگر باشد متن است، ولی می‌شود اصلاً ندهی‌اش». حالا greet("Ali") قبول می‌شود. داخل تابع با if (title) می‌پرسیم «لقبی داده شده؟» و فقط آن وقت از آن استفاده می‌کنیم.

<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">
function greet(name: string, title?: string): string {
  if (title) {
    return title + " " + name;
  }
  return name;
}
show(greet("Sara", "Dr"));
show(greet("Ali"));
</script>

چرا آن if لازم است؟ چون وقتی ورودی اختیاری را ندهی، داخل تابع خالی است: مقدارش undefined است. و undefined نه length دارد، نه toUpperCase. در جاوااسکریپت این‌جا برنامه وسط کار می‌ایستاد. تایپ‌اسکریپت زودتر می‌گوید: «city ممکن است undefined (خالی) باشد». اجرا کن و خط قرمز را ببین. درمانش همان if (city) است.

<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">
function sign(name: string, city?: string): string {
  return name + " from " + city.toUpperCase();
}
show(sign("Sara", "Yazd"));
show(sign("Ali"));
</script>

راه دوم: مقدار پیش‌فرض. اگر برای ورودی یک مقدار همیشگی داری، آن را با = جلویش بنویس: greeting: string = "Hello". اگر کسی این ورودی را ندهد، همان "Hello" جایش می‌نشیند. خوبی‌اش این است که ورودی دیگر هیچ‌وقت خالی نیست و 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">
function welcome(name: string, greeting: string = "Hello"): string {
  return greeting + ", " + name;
}
show(welcome("Sara"));
show(welcome("Ali", "Hi"));
</script>

یک قانون: ورودی‌های اختیاری آخر می‌آیند. تایپ‌اسکریپت ورودی‌ها را به ترتیب پر می‌کند. اگر اولی اختیاری و دومی اجباری باشد، با greet("Ali") معلوم نیست "Ali" کدام است. برای همین این خط اول قرمز می‌شود: ورودی اجباری نمی‌تواند بعد از ورودی اختیاری بیاید.

<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">
function greet(title?: string, name: string): string {
  return name;
}
show(greet("Dr", "Sara"));
</script>

بعضی تابع‌ها جوابی برنمی‌گردانند؛ فقط کاری می‌کنند: چیزی نشان می‌دهند یا جعبه‌ای را عوض می‌کنند. نوع جواب این تابع‌ها void است، یعنی «هیچ». جعبه‌ی دکمه هم همین‌طور است: function press(): void. دکمه را چند بار بزن.

<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>

<button id="go" onclick="press()">Add 5</button>
<pre id="out"></pre>

<script type="text/typescript">
let score: number = 0;

function addPoints(points: number): void {
  score = score + points;
  show("Score", score);
}

function press(): void {
  addPoints(5);
}
</script>

void هم جلوی یک اشتباه را می‌گیرد: این‌که جواب تابعی را که جوابی ندارد در جعبه بگذاری. در جاوااسکریپت total این‌جا بی‌صدا undefined می‌شد و بعد total + 1 می‌شد NaN. این‌جا خط قرمز می‌گوید: این‌جا باید number باشد، ولی مقدارت void است.

<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">
function printSum(a: number, b: number): void {
  show(a + b);
}
const total: number = printSum(2, 3);
show(total + 1);
</script>

کدام را کی انتخاب کنی؟ اگر تابع بدون آن ورودی هم کار می‌کند، ? بگذار و با if بررسی‌اش کن. اگر یک مقدار همیشگی دارد، پیش‌فرض بده. و هر دو را با هم نگذار: mark?: string = "!" قبول نمی‌شود، چون پیش‌فرض خودش ورودی را اختیاری می‌کند.

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

چالش‌ها

  1. Make It Optional (آسان)
  2. The Usual Price (آسان)
  3. The Button Box (متوسط)
  4. Maybe a City (متوسط)
  5. Find the Red Lines (سخت)
  6. The Price Tag (سخت)

بیشتر بخوان

← جلسه‌ی 6: ورودی‌های نوع‌دار جلسه‌ی 8: آرایه‌های نوع‌دار →