JavaScript

JavaScript Function Call
خاصية call مع الدوال في لغة جافا سكريبت

في هذا الدرس سوف نتعرف علي طرق تنفيذ الدوال التي قمنا بأنشائها 

التاريخ

11 نوفمبر 2021

الدروس

146

المستوى

العامة

اللغة

انجليزي

المشاهدات

1461

المواضيع

24
الشروحات chevron_left JavaScript Function Call chevron_left JavaScript

JavaScript Function Call
إستدعاء الدوال في لغة جافا سكريبت

</> this Keyword in JavaScript functions
كلمة this مع الدوال في لغة جافا سكريبت

ماهي this Keword ؟

this Keyword لها إستخدامات في لغة جافا سكريبت عديدة وسوف نتطرق للحديث  بالتفصيل في الدروس المقبلة ولكن سوف نتحدث الآن عن علاقتها بالدوال function في لغة جافا سكريبت.

  • بداخل الكأئنات object method تشير this Keyword إلي الكائن بجميع خصائصة.
  • بدون الكائنات أو الدوال this Keyword تحمل كائن window.
  • تستخدم this داخل الدالة function  في الوضع الصارم "use strict" ، وتكون قيمتها الإفتراضية undefined غير معرف.
  • تستخدم this بداخل الدوال function  مع Javascript Event ، وهي تساوي في هذه الحالة عنصر HTML Element الذي يحتوي علي Event الذي  يستدعي هذة الدالة.
this Element

تغيير لون المعنصر عن طريق جافا سكريبت بأستخدام Event وبأستخدام this Keyword.

<h1 id="heading" onclick="this.style.color = 'red' ">JavaScript Functions</h1>
this Element

تغيير لون العنصر وتغيير النص عن طريق جافا سكريبت بأستخدام Event وبأستخدام this Keyword.

heading.onmouseover = function(){
  this.innerHTML = "Text Changed..";
  this.style.color = "blue";
}
this Element Inline

تغيير لون العنصر وتغيير النص عن طريق جافا سكريبت بأستخدام Event وبأستخدام this Keyword.

<h1 id="heading">JavaScript Functions</h1>
<p class="para" onclick="myFunction(this)"> JavaScript Functions 1</p>
<p class="para" onclick="myFunction(this)"> JavaScript Functions 2</p>
<p class="para" onclick="myFunction(this)"> JavaScript Functions 3</p>
<script>
function myFunction(element){
  element.style.color = "red";
}
</script>
this with use strict

this keyword تكون قيمتها في الوضع الصارم undefind.

function myFunction(){
  "use strict";
   document.write(this);
}
myFunction();
this without use strict

this keyword تكون قيمتها بدون الوضع الصارم تساوي object window.

function myFunction(){
  return this;
}
let x = myFunction();
document.write(x.innerWidth);
document.write("<br>");
document.write(this.innerWidth);
this keyword with object

تستخدم this keyword وهي تعود علي الكائن الاصلي الزي تكتب بداخلة كلمة this.

const student = {
  firstName: "Mohamed",
  lastName: "Ahmed",
  id: 99852,
  fullName : function() {
    return this.firstName + " " + this.lastName;
  }
};

document.write(student.fullName());
ملاحظة
this : هي عبارة عن كلمة مفتاحية keyword ليست متغير variable أي انها لا يمكن تغيير القيمة المخزنة بداخلها

</> call() Method With JavaScript Function
خاصية ()call مع الدالة في لغة جافا سكريبت.

في لغة جافا سكريبت يُمكن إستخدام دالة بداخل أي كائن Object تَكون قيمة لأي خاصية Property من خصائص هذا الكائن ، ويمكن إستخدام this Keyword وتشير this إلي الكائن الذي يحمل هذه الدالة Function ، ويمكن تمرير قيم مُختلفة لهذا الكائن Object عن طريق دالة ()call.

Call() With Object

في هذا المثال يمكن إستدعاء Object عن طريق دالة ()call وتمرير له Object كائن آخر ليقوم الكائن الأول بدمج النصوص عن طريق this Keyword.

const student = {
  fullName: function() {
    return this.firstName + " " + this.lastName;
  }
}
const first = {
  firstName:'Ahmed',
  lastName:'Mohamed'
}
document.write(student.fullName.call(first));
Call() With Object

في هذا المثال يمكن إستدعاء Object عن طريق دالة ()call وتمرير له Object كائن آخرمع تمرير عوامل Arguments للدالة الداخلية.

const student = {
  fullName: function(age,country) {
    return this.firstName + " " + this.lastName + " " + age + " " + country;
  }
}
const first = {
  firstName:'Ahmed',
  lastName:'Mohamed'
}
document.write(student.fullName.call(first,25,'Egypt'));

</> JavaScript Function Call
خاصية call() method مع الدالة في لغة جافا سكريبت

تستخدم خاصية call() Method في كتابة خاصية من خصائص object بكائنات مُختلفة.

في لغة الجافا سكريبت جميع الخصائص المُدمجة built-in هي عبارة في الأصل عن كائن بداخلة دالة function.

يُمكنك الآن تعلم طريقة صناعة الخصائص المُدمجة مع لغة جافاسكريبت بطريقة سهلة وبسيطة ، سوف نقوم الآن بإنشاء خاصية  تقوم بدمج نصين معاً عن طريق دمج الكائنات Objects مع الدوال Functions.

Create Method

تم إنشاء Object بداخله دالة Function ، ثم بعد ذلك تم كتابة إسم Object متبوعاً بالدالة Function.

const concatenate = {
add: function(text1,text2) {
    return text1 + text2;
  }
}
document.write(concatenate.add("ABC","DEF"));
Create Method Use this

تم إنشاء Object بداخله دالة Function ، ثم بعد ذلك تم كتابة إسم Object متبوعاً بالدالة Function.

const type= {
check: function(value) {
    return typeof value;
  }
}
document.write(type.check("ABC"));
document.write("<br>");
document.write(type.check(3455));
معلومات تهمك
  • لا تنسى تقييم الدروس لكي نُحدّث المُحتوى باستمرار حتى ينال إعجابك.
  • لا تنسى مشاركة الموقع مع أصدقاءك حتى تعمّ الفائدة وتكون سبب في نفعهم.
مشاركة
0
0
0
0
عدد المشاركات
هل هذه المعلومات نالت إعجابك ؟
0
0
عدد التقييمات