انتقل إلى محتوى الدرس
فقرة فرعية #3 من درس: Arrow Function

this

سنتعرف في هذا الدرس علي نوع جديد من الدوال وهو arrow function

1 صفوف تعليمية
جافا سكريبت
3,058 مشاهدة

شرح صفوف ومحتوى الفقرة

الصف 1 شرح توضيحي

تختلف القيم التي تقوم بأرجاعها كلمة 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);

جميع فقرات هذا الدرس (Arrow Function)