✅ أولًا: ما هي this
؟
this
هي كلمة مفتاحية تشير إلى الكائن (Object) الذي يستدعي الدالة (أو السياق الذي نُفذت فيه الدالة).
لكن معناها يتغير حسب السياق الذي تُستدعى فيه.
🧭 الحالات الأساسية التي يتغير فيها this
:
السياق | ماذا تشير this إليه؟ |
---|---|
في دالة عادية (strict mode) | undefined |
في دالة عادية (بدون strict) | الكائن window |
في دالة داخل كائن | الكائن نفسه |
في Class | الكائن الذي تم إنشاؤه |
في event handler في DOM | العنصر الذي أطلق الحدث |
في arrow function | لا تملك this خاص بها، بل ترثه من السياق المحيط |
عند استخدام call أو apply أو bind | يتم تحديد this يدويًا |
في setTimeout أو setInterval | this = window (إلا إذا استخدمت arrow function أو bind |
📘 1. this
في الدوال العادية (Functions)
function showThis() {
console.log(this);
}
showThis();
JavaScriptالنتيجة:
- في الوضع العادي (non-strict):
this = window
- في الوضع الصارم (strict mode):
this = undefined
📘 2. this
داخل كائن (Object Method)
const user = {
name: "Ali",
sayHello: function () {
console.log("Hello " + this.name);
}
};
user.sayHello(); // Hello Ali
JavaScriptهنا this
تشير إلى الكائن user
لأنه هو من استدعى الدالة.
📘 3. this
في Arrow Function
const user = {
name: "Sara",
sayHi: () => {
console.log("Hi " + this.name);
}
};
user.sayHi(); // Hi undefined
JavaScript❌ الخطأ الشائع:
this
فيarrow function
لا تشير إلىuser
، بل إلىthis
الخارجي (في المتصفح → window).✅ الحل:
const user = {
name: "Sara",
sayHi: function () {
const arrow = () => console.log("Hi " + this.name);
arrow();
}
};
user.sayHi(); // Hi Sara
JavaScriptلأن الـ arrow function أخذت
this
من الدالة العادية المحيطة.
📘 4. this
في setTimeout
const obj = {
name: "Omar",
greet: function () {
setTimeout(function () {
console.log("Hello " + this.name);
}, 1000);
}
};
obj.greet(); // Hello undefined
JavaScript
this
داخلsetTimeout
تشير إلىwindow
.
✅ الحل 1: استخدم Arrow Function
const obj = {
name: "Omar",
greet: function () {
setTimeout(() => {
console.log("Hello " + this.name);
}, 1000);
}
};
obj.greet(); // Hello Omar
JavaScript✅ الحل 2: استخدام bind
setTimeout(function () {
console.log("Hello " + this.name);
}.bind(this), 1000);
JavaScript📘 5. this
داخل Class
class Car {
constructor(brand) {
this.brand = brand;
}
showBrand() {
console.log("Brand: " + this.brand);
}
}
const myCar = new Car("BMW");
myCar.showBrand(); // Brand: BMW
JavaScriptهنا
this
تشير إلى الكائن الذي تم إنشاؤه (myCar)
📘 7. this
في DOM Event Handler
<button id="btn">Click me</button>
<script>
document.getElementById("btn").addEventListener("click", function () {
console.log(this); // الزر نفسه
});
</script>
JavaScript
this
تشير إلى العنصر (button) الذي نفذ الحدث.
⚠️ ملاحظات عامة:
this
لا تعتمد على مكان تعريف الدالة، بل على مكان استدعائها.- الدوال العادية تتغير فيها
this
بسهولة. - الدوال السهمية (Arrow) ثابتة في this وترثها من السياق المحيط.
🧠 متى أستخدم arrow ومتى function عادية؟
الحالة | الأنسب |
---|---|
داخل كائن أو Class | دالة عادية |
داخل Callback أو setTimeout | Arrow Function |
عند الحاجة إلى this من الخارج | Arrow |
عند الرغبة في تغيير this بـ bind/call | دالة عادية |