شرح صفوف ومحتوى الفقرة
تختلف القيم التي تقوم بأرجاعها كلمة this في ال arrow function عن ال regular function وهذا لأن في ال arrow function غير ملزم كتابتها
اما في regular function فهي تشير الي ال object الذي قام باستدعاء الدالة ويمكن ان يكون اي نوع ( window , document , button )
وفي ال arrow function دائما تشير الي object الذي قام بأنشاء الدالة خلاله فقط
في المثال الاول يتم ارجاع اثنين من ال object المختلفتين وهما ( window , button )
في المثال التالي يتم ارجاع قيمتين متطابقتين وهو window وهذا لأن هذا ال object هو ال owner الخاص بالدالة
Regular Function
HTML
/* 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
HTML
/* 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);