✅ أولًا: ما هي 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 AliJavaScriptهنا this تشير إلى الكائن user لأنه هو من استدعى الدالة.
📘 3. this في Arrow Function
const user = {
name: "Sara",
sayHi: () => {
console.log("Hi " + this.name);
}
};
user.sayHi(); // Hi undefinedJavaScript❌ الخطأ الشائع:
thisفيarrow functionلا تشير إلىuser، بل إلىthisالخارجي (في المتصفح → window).✅ الحل:
const user = {
name: "Sara",
sayHi: function () {
const arrow = () => console.log("Hi " + this.name);
arrow();
}
};
user.sayHi(); // Hi SaraJavaScriptلأن الـ arrow function أخذت
thisمن الدالة العادية المحيطة.
📘 4. this في setTimeout
const obj = {
name: "Omar",
greet: function () {
setTimeout(function () {
console.log("Hello " + this.name);
}, 1000);
}
};
obj.greet(); // Hello undefinedJavaScript
thisداخلsetTimeoutتشير إلىwindow.
✅ الحل 1: استخدم Arrow Function
const obj = {
name: "Omar",
greet: function () {
setTimeout(() => {
console.log("Hello " + this.name);
}, 1000);
}
};
obj.greet(); // Hello OmarJavaScript✅ الحل 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: BMWJavaScriptهنا
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 | دالة عادية |