English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Object

JavaScript Function

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

طرق العنصر في JavaScript

Methodis a method associated with an object, or a related function; a method is an object that is a property of a function.

The definition of a method is the same as that of a regular function, the difference being that they must be assigned as properties of an object.

Access JavaScript methods

To retrieve an object method, you will call it in the same way as a regular function, but attach it to the object variable.

// إنشاء جسم
var user = {
  firstName: "Seagull",
  lastName : "an",
  age: 22,
  location: "New Delhi",
  getName : function() {
 return this.firstName + " " + this.lastName;
  }
};
// Access the getName() method
user.getName();
اختبار لرؤية‹/›

If you access a method without parentheses, it will return the function definition:

user.getName;
اختبار لرؤية‹/›

UsingthisAs an object reference

JavaScript has a special keyword this that you can use in methods to refer to the current object.

You may have noticed that our methods are a bit strange. Take this for example:

  getName: function() {
   return this.firstName + " " + this.lastName;
  }

The this keyword refers to the current object within which the code is written - so in this case, this is equivalent touser.

In other words, this.firstName representsThis objectthe firstName property.

You can useJS KeywordsTutorialIn JSLearn more about the this keyword.

Add a new method

To add a new method to an object, you can use the assignment operator (=) to assign a new function to the property.

This example adds the "greet" method to the user object:

user.greet = function() {
    return "Hello World";
};
اختبار لرؤية‹/›

Getters and Setters

Getters and setters were introduced in ECMAScript 5 (2009).

A getter is a method for getting a specific property value.

A setter is a method for setting a specific property value.

You can define getters and setters on any predefined core object or user-defined object that supports adding new properties.

JavaScript Getter (get keyword)

This example uses get locالخاصية كـlocationقيمة الخاصية:

// create an object
var user = {
  firstName: "Seagull",
  lastName: "Anna",
  age: 22,
  location: "New Delhi",
  get loc() {
     return this.location;
  }
};
// عرض البيانات من الجسم
document.getElementById("para").innerHTML = user.loc;
اختبار لرؤية‹/›

مزود JavaScript (كلمة المفتاح set)

يستخدم هذا المثال set locالخاصية كـlocationقيمة الخاصية:

// إنشاء جسم
var user = {
  firstName: "Seagull",
  lastName: "Anna",
  age: 22,
  location: "New Delhi",
  set loc(x) {
     this.location = x;
  }
};
// استخدام setter لتعيين خاصية الجسم
user.loc = "Goa";
// عرض البيانات من الجسم
document.getElementById("para").innerHTML = user.location;
اختبار لرؤية‹/›

ما هو الفرق بين الدالة Function و getter؟

تظهر هذه الأمثلة الاثنتين الفرق بين function و getter:

مثال 1 (استخدام الدالة):
// إنشاء جسم
var user = {
  firstName: "Seagull",
  lastName: "Anna",
  age: 22,
  location: "New Delhi",
  fullName: function() {
      return this.firstName + " " + this.lastName;
  }
};
// عرض البيانات من الجسم
document.getElementById("para").innerHTML = user.fullName();
اختبار لرؤية‹/›
مثال 2 (استخدام المُستقبل):
// إنشاء جسم
var user = {
  firstName: "Seagull",
  lastName: "Anna",
  age: 22,
  location: "New Delhi",
  get fullName() {
     return this.firstName + " " + this.lastName;
  }
};
// عرض البيانات من الجسم
document.getElementById("para").innerHTML = user.fullName;
اختبار لرؤية‹/›

مثال 1 استخدام fullName كـدالةالوصول: user.fullName().

مثال 2 استخدام fullName كـالخصائصالوصول: user.fullName.

استخدام المُستقبلين والمزودين:

  • يقدم بناءً بسيطًا

  • يُمكنه جعل قواعد السلوك والمستقبلين والمزودين متشابهة

  • يضمن جودة أفضل للبيانات

  • مفيدة جدًا في معالجة الخلفية

Object.defineProperty()

يمكن استخدام دالة Object.defineProperty() أيضًا لإضافة المُستقبلين والمُزودين.

القواعد النحوية:
تعريف خاصية في الجسم، prop، {value: value})

لنأخذ مثالاً على "مفهوم المعدتور":}}

var counter = {i : 0};
Object.defineProperty(counter, "increment", { 
   get: function() {this.i++;},
});
Object.defineProperty(counter, "decrement", { 
   get: function() {this.i--;},
});
Object.defineProperty(counter, "reset", { 
   get: function() {this.i = 0;},
});
Object.defineProperty(counter, "add", {
   set: function (value) {this.i += value;}
});
Object.defineProperty(counter, "subtract", {
   set: function (value) {this.i -= value;}
});
counter.reset;
counter.add = 25;
counter.increment;
اختبار لرؤية‹/›