خلاصه فارسی “آموزش جاوا اسکریپت – جلسه ۶”


مقدمه

در این جلسه، بهزاد فرهادی مفاهیم پایه‌ای توابع در جاوا اسکریپت را معرفی می‌کند. درس شامل تعریف و استفاده از توابع، مفهوم اسکوپ (متغیرهای سراسری و محلی) و تمرین‌هایی برای درک تفاوت بین آن‌ها است. این آموزش به روش عملی ارائه شده و مثال‌های واضحی برای درک بهتر مفاهیم ارائه می‌شود.


موضوعات اصلی مطرح‌شده

۱. معرفی توابع

  • توابع در جاوا اسکریپت امکان استفاده مجدد از کد و سازمان‌دهی بهتر را فراهم می‌کنند.
  • مثال: getElementById() یک تابع است که یک عنصر را از DOM دریافت می‌کند.
  • توابع را می‌توان چندین بار فراخوانی کرد که باعث بهینه‌سازی و کاهش تکرار کد می‌شود.

۲. مفهوم استفاده مجدد (Reusability)

  • توابع با کارهای روزمره مانند دم کردن چای مقایسه می‌شوند.
  • به جای تکرار مراحل، توابع این مراحل را در یک گروه قرار می‌دهند.
  • این رویکرد خوانایی کد، اشکال‌زدایی و کارایی را بهبود می‌بخشد.

۳. تعریف یک تابع

  • برای تعریف یک تابع از کلمه کلیدی function استفاده می‌شود.
  • مثال: ساخت یک تابع با نام calculator برای جمع دو عدد.
  • ساختار:
				
					function calculator(number1, number2) {
    return number1 + number2;
}
				
			
  • پارامترها (number1 و number2) ورودی‌هایی هستند که محاسبات را پویا می‌کنند.

۴. اجرای یک تابع (فراخوانی تابع)

  • توابع باید صدا زده شوند تا اجرا شوند.
  • مثال از فراخوانی تابع calculator:
				
					console.log(calculator(3, 4)); // خروجی: ۷
				
			
  • ورودی‌های متفاوت، خروجی‌های متفاوتی تولید می‌کنند.

۵. درک مفهوم اسکوپ (متغیرهای سراسری و محلی)

  • متغیرهای داخل یک تابع فقط درون همان تابع قابل دسترسی هستند (اسکوپ محلی).
  • متغیرهای تعریف‌شده در خارج از توابع به صورت سراسری قابل دسترسی هستند.
  • مثال:
				
					let x = 10;  // متغیر سراسری
function exampleFunction() {
    let y = 5;  // متغیر محلی
    console.log(x + y);
}
exampleFunction(); // خروجی: ۱۵
				
			

۶. بهبود عملکرد تابع

  • به جای استفاده از اعداد ثابت، از متغیرهای پویا (let x, let y) استفاده می‌شود.
  • مثال: دریافت ورودی از کاربر به جای مقداردهی ثابت.

نتیجه‌گیری

در این جلسه، بهزاد فرهادی به‌خوبی نحوه تعریف، استفاده و مزایای توابع در جاوا اسکریپت را توضیح می‌دهد. تمرکز آموزش بر روی سازمان‌دهی صحیح توابع، استفاده مجدد و درک مفهوم اسکوپ است. در پایان، فراگیران مهارت لازم برای نوشتن و استفاده از توابع را به دست می‌آورند که منجر به کدنویسی تمیزتر و بهینه‌تر در جاوا اسکریپت خواهد شد.

Summary of “JavaScript by Behzad Farhadi – Session 06”


Introduction

In this session, Behzad Farhadi introduces the fundamental concepts of functions in JavaScript. The lesson covers defining and using functions, the concept of scope (global and local variables), and practical exercises to differentiate between them. The tutorial provides a hands-on approach with clear examples, making it easier for beginners to understand how functions work and how they can be reused.


Main Topics Discussed

1. Introduction to Functions

  • Functions in JavaScript allow code reuse and organization.
  • Example: getElementById() is a function that retrieves an element from the DOM.
  • Functions can be called multiple times, making them efficient and reducing redundancy.

2. The Concept of Reusability

  • Functions are compared to real-life activities like making tea.
  • Instead of repeatedly listing steps, a function groups them together.
  • This approach improves code readability, debugging, and efficiency.

3. Defining a Function

  • The function keyword is used to declare a function.
  • Example: A function named calculator is created to sum two numbers.
  • Structure:
				
					function calculator(number1, number2) {
    return number1 + number2;
}
				
			

Parameters (number1 and number2) are inputs that allow dynamic calculations.

4. Function Execution (Calling a Function)

  • Functions must be called to execute.
  • Example of calling the calculator function:
				
					console.log(calculator(3, 4)); // Output: 7
				
			

 Different inputs produce different results.

5. Understanding Scope (Global vs. Local Variables)

  • Variables inside a function are only accessible within that function (local scope).
  • Variables declared outside functions are globally accessible.
  • Example:
				
					let x = 10;  // Global variable
function exampleFunction() {
    let y = 5;  // Local variable
    console.log(x + y);
}
exampleFunction(); // Output: 15
				
			

6. Enhancing the Functionality

  • Instead of hardcoding numbers, dynamic variables (let x, let y) are used.
  • Example: Accepting user input instead of fixed values.

Conclusion

In this session, Behzad Farhadi effectively explains JavaScript functions, their importance, and their implementation. The tutorial focuses on structuring functions properly, reusability, and understanding scope. By the end, learners are equipped with a solid foundation in writing and calling functions, improving their ability to write clean and efficient JavaScript code.

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *