جلسهی 13: چیز در چیز Things in Things
دادهی واقعی تو در تو است: شیئی که داخلش شیء دیگری است، و آرایهای از شیءها. یاد میگیری برای اینها شکل بنویسی و تابعی بسازی که یک شکل میگیرد و یک شکل برمیگرداند.
در این جلسه یاد میگیری
- در پایان این جلسه میتوانی آرایهای از شیءها بسازی:
Player[]. - در پایان این جلسه میتوانی شکلی بنویسی که یک فیلدش خودش یک شکل دیگر است.
- در پایان این جلسه میتوانی تابعی بنویسی که یک شکل میگیرد و یک شکل برمیگرداند.
- در پایان این جلسه میدانی اول شکلهای کوچک را میسازیم و بعد شکل بزرگ را از آنها.
درس
مینا امتیاز تیم را جمع میزند. بازیکنها در یک آرایهاند. یک نفر عجله داشته و بهجای یک شیء کامل، فقط اسم "Reza" را به آرایه اضافه کرده است. در جاوااسکریپت جمع امتیازها بیصدا میشود NaN، چون یک متن ساده score ندارد. هر چه داده تو در توتر شود، این اشتباهها بیشتر و پیدا کردنشان سختتر میشود. امروز برای دادهی تو در تو شکل مینویسیم.
یادت هست number[] یعنی آرایهای از عدد؟ با شکل خودت هم همین کار را میشود کرد: Player[] یعنی «آرایهای که هر خانهاش یک Player است». داخل حلقه، تایپاسکریپت میداند p یک Player است، پس p.score حتماً عدد است.
<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 team: Player[] = [
{ name: "Sara", score: 10 },
{ name: "Ali", score: 7 },
];
let sum: number = 0;
for (const p of team) {
sum = sum + p.score;
}
show("Total", sum);
</script>
حالا همان اشتباه مینا. در یک Player[] فقط Player جا میشود. نه یک متن ساده، نه شیئی که یک فیلدش جا افتاده است. اجرا کن: هر دو push خط قرمز میگیرند، و NaN هیچوقت به صفحه نمیرسد.
<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 team: Player[] = [{ name: "Sara", score: 10 }];
team.push("Reza");
team.push({ name: "Mina" });
show(team.length);
</script>
یک فیلد میتواند خودش یک شیء باشد. قهرمان بازی یک جا دارد: x و y. اول شکل کوچک را بساز (Point)، بعد در شکل بزرگ از آن استفاده کن: pos: Point. برای رسیدن به داخل، نقطه پشت نقطه میگذاری: hero.pos.x.
<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 Point {
x: number;
y: number;
}
interface Hero {
name: string;
pos: Point;
}
const hero: Hero = { name: "Mina", pos: { x: 2, y: 5 } };
hero.pos.x = hero.pos.x + 1;
show(hero.name, hero.pos.x, hero.pos.y);
</script>
تایپاسکریپت تا ته شکل را بررسی میکند، هر چقدر هم تو در تو باشد. اینجا pos فیلد y ندارد، و خط بعد سراغ z میرود که اصلاً در Point نیست. اجرا کن و ببین خط قرمز دقیقاً میگوید مشکل در کدام لایه است.
<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 Point {
x: number;
y: number;
}
interface Hero {
name: string;
pos: Point;
}
const hero: Hero = { name: "Mina", pos: { x: 2 } };
show(hero.pos.z);
</script>
تابع میتواند یک شکل بگیرد و یک شکل برگرداند. move یک Point میگیرد و یک Point تازه میسازد. نوع خروجی (: Point) یک قول است: اگر در return فیلد y را جا بیندازی، همانجا خط قرمز میبینی. دقت کن: نقطهی اول دست نخورده میماند.
<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 Point {
x: number;
y: number;
}
function move(p: Point, dx: number): Point {
return { x: p.x + dx, y: p.y };
}
const a: Point = { x: 1, y: 4 };
const b: Point = move(a, 3);
show(a.x, a.y);
show(b.x, b.y);
</script>
تابع میتواند یک آرایه از شکلها هم بگیرد. best یک Player[] میگیرد و یک Player برمیگرداند: کسی که بیشترین امتیاز را دارد. map هم نوع را نگه میدارد: team.map((p) => p.name) خودش یک 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;
score: number;
}
function best(list: Player[]): Player {
let top: Player = list[0];
for (const p of list) {
if (p.score > top.score) {
top = p;
}
}
return top;
}
const team: Player[] = [
{ name: "Sara", score: 10 },
{ name: "Ali", score: 25 },
];
show(best(team).name);
show(team.map((p) => p.name).join(", "));
</script>
مثالهای این جلسه
- مثال JavaScript: <script type="text/typescript"> — چیز در چیز
- مثال JavaScript: <script type="text/typescript"> — چیز در چیز 2
- مثال JavaScript: <script type="text/typescript"> — چیز در چیز 2 3
- مثال JavaScript: <script type="text/typescript"> — چیز در چیز 2 3 4
- مثال JavaScript: <script type="text/typescript"> — چیز در چیز 2 3 4 5
- مثال JavaScript: <script type="text/typescript"> — چیز در چیز 2 3 4 5 6
چالشها
- The Team Total (آسان)
- Where Is the Hero? (آسان)
- Type the Total (متوسط)
- Move the Point (متوسط)
- Find the Red Lines (سخت)
- The Best Player (سخت)