في هذا الدرس 4تصفّح المواضيع
Arrow Function
Arrow Function
- تم انشاء ال arrow function في الاصدار ES6
- تسمح لنا arrow function بأنشاء دوال عن طريق كتابة احرف قليلة
- سميت ب arrow function لأنها تشبه " السهم "
- إذا لم يوجد معاملات يتم ترك الأقواس فارغة (ولكن يجب كتابتها):
Syntax
object = ( ) => return ;
Examples
Examples
Before
With Arrow Function
Arrow Functions Return Value
Arrow Function With Parameters
Arrow Function Without Parentheses
this
this
تختلف القيم التي تقوم بأرجاعها كلمة this في ال arrow function عن ال regular function وهذا لأن في ال arrow function غير ملزم كتابتها
اما في regular function فهي تشير الي ال object الذي قام باستدعاء الدالة ويمكن ان يكون اي نوع ( window , document , button )
وفي ال arrow function دائما تشير الي object الذي قام بأنشاء الدالة خلاله فقط
في المثال الاول يتم ارجاع اثنين من ال object المختلفتين وهما ( window , button )
في المثال التالي يتم ارجاع قيمتين متطابقتين وهو window وهذا لأن هذا ال object هو ال owner الخاص بالدالة
Regular Function
/* Regular Function: */
hello = function() {
document.getElementById("demo").innerHTML += this;
}
/* The window object calls the function: */
window.addEventListener("load", hello);
/* A button object calls the function: */
document.getElementById("btn").addEventListener("click", hello);
Arrow Function
/* Arrow Function: */
hello = () => {
document.getElementById("demo").innerHTML += this;
}
/* The window object calls the function: */
window.addEventListener("load", hello);
/* A button object calls the function: */
document.getElementById("btn").addEventListener("click", hello);
Arrow Functions
Arrow Functions
ال arrow function هو عبارة عن اسلوب يستخدم في كتابة الدوال لتقليل وتسهيل عملية انشاء دالة وذلك عن طريق استبدال الاقواس المتعرجة "{ } " ب علامة تشبه السهم "<=" ولذلك تسمي " arrow function "
- لايمكننا استخدام كلمة this في ال arrow functions
- لا يمكننا استدعائها في الأسطر التي تسبق عملية التعريف الخاصة بها
- لا تحتاج لأنشائها لكتابة كلمة "function "
Example
سجّل الدخول لحفظ تقدمك
تابع إنجاز المواضيع والمصطلحات والمهام، وكمّل من آخر خطوة على أي جهاز.