متغیرهای آرایه امکان نگهداری چند عنصر با انواع دادههای مختلف را دارند. در هنگام تعریف یک متغیر از نوع آرایه، مقادیر آرایه درون علامت کروشه نوشته میشوند. یک آرایه میتواند دارای عناصری با نوع داده یکسان یا متفاوت باشد.
هر یک از عناصر آرایه دارای اندیس هستند. اندیس عناصر آرایه از صفر شروع میشود، بنابراین اگر یک آرایه دارای 5 عنصر باشد، اندیس عنصر اول صفر و اندیس عنصر آخر 4 خواهد بود. برای دسترسی به عناصر آرایه از اندیس عناصر استفاده میشود. شکل زیر نمایی از عناصر آرایه cars و مقادیر آنها را نمایش میدهد:
Hyundai
BMW
Volvo
Saab
Opel
مقدار عناصر
4
3
2
1
0
اندیس عناصر
برای دسترسی به عنصر اول آرایه از عبارت cars[0] و برای دسترسی به عنصر چهارم از عبارت cars[4] استفاده میشود.
صفحه ۲۰۷
صفحه ۲۰۸
اشیا در جاوااسکریپت
شیء (Object) یک ساختار داده است که برای ذخیرۀ مجموعهای از دادههای مرتبط، بهصورت جفتهای کلید: مقدار استفاده میشود، در ساختار داده شیء، مجموعهای از کلیدها (keys) به مقادیر (values) متناظر خود اشاره میکنند. اشیا برای ذخیره و سازماندهی دادهها و رفتارهای یک موجودیت استفاده میشوند. که ساختاری شبیه به دیکشنری در پاتیون دارد.
صفحه ۲۰۹
در تعریف فوق، کلمه کلیدی this به شیء جاری (person) اشاره میکند و برای دسترسی به ویژگیهای شیء جاری بهکار میرود. این متد اطلاعات شیء person را به محل فراخوانی برمیگرداند.
برای فراخوانی متد شیء، ابتدا نام شیء (Person) و سپس نام متد(displayInfo) به همراه علائم () نوشته میشود.
رشتهای که توسط متد displayInfo() به محل فراخوانی برگردانده (return) میشود، باید درون علامت backtick قرار گیرد. این علامت در گوشه بالای صفحه کلید، زیر دکمه esc واقع شده است.
صفحه ۲۱۰
ساختارهای شرطی و حلقه در جاوااسکریپت
ساختارهای شرطی و حلقه از اجزای اساسی زبانهای برنامهنویسی، از جمله جاوااسکریپت هستند. ساختارهای شرطی منطق برنامه را بر اساس شرایط خاص کنترل میکنند و ساختارهای حلقه عملیات خاصی را بهصورت تکراری انجام میدهند. این بخش به معرفی این ساختارها میپردازد.
دستورات شرطی If
یک دستور شرطی امکان اجرای مجموعهای از دستورات را به ازای برقراری یک شرط خاص فراهم میکند. بسته به نیاز برنامه، دستور if میتواند به سه صورت نوشته شود. جدول 20 شکل کلی ساختار شرطی if را در سه حالت مختلف نمایش میدهد.
جدول ۲۰: انواع ساختارهای شرطی if
else-if
if-else
if ساده
if (condition1) { statement1 } else if (condition2) { statement2 } else { statement3 }
if (condition){ Statement1 }else{ Statement2 }
if (condition){ statements }
Page 206
Arrays
Array variables can hold several elements with different data types. When defining a variable of the array type, the values of the array are written inside square brackets. An array can have elements of the same or different data types.
Each element of an array has an index. The index of the array elements starts from zero, so if an array has 5 elements, the index of the first element is zero and the index of the last element is 4. To reach the elements of the array, the index of the elements is used. The following figure shows the elements of the array cars and their values:
Hyundai
BMW
Volvo
Saab
Opel
The value of the elements
4
3
2
1
0
The index of the elements
To reach the first element of the array the expression cars[0] is used, and to reach the fourth element the expression cars[4] is used.
Page 207
Page 208
Objects in JavaScript
An object is a data structure used to store a set of related data in the form of key: value pairs; in the object data structure, a set of keys point to their matching values. Objects are used to store and organise the data and behaviours of an entity. It has a structure similar to the dictionary in Python.
Page 209
In the definition above, the keyword this points to the current object (person) and is used to reach the properties of the current object. This method returns the information of the object person to the place of the call.
To call the method of an object, first the name of the object (Person) and then the name of the method (displayInfo) are written with the () signs.
The string that is returned by the method displayInfo() to the place of the call must be put inside the backtick sign. This sign is located at the top corner of the keyboard, under the esc key.
Page 210
Conditional structures and loops in JavaScript
Conditional structures and loops are basic parts of programming languages, including JavaScript. Conditional structures control the logic of the program based on certain conditions, and loop structures do certain operations repeatedly. This section introduces these structures.
The If conditional statements
A conditional statement makes it possible to run a set of statements when a certain condition holds. Depending on the needs of the program, the if statement can be written in three forms. Table 20 shows the general form of the if conditional structure in three different cases.
Table 20: Types of if conditional structures
else-if
if-else
A simple if
if (condition1) { statement1 } else if (condition2) { statement2 } else { statement3 }