ÙÙ
ا ÙÙ
ÙÙÙØ§ تعÙÙÙ Ø®Ø§ØµÙØ© ÙØ¯Ø§ÙØ© اÙclass Ø°Ø§ØªÙØ§, ÙÙÙØ³ ÙÙ "prototype" Ø§ÙØ®Ø§Øµ Ø¨ÙØ§. Ù
Ø«Ù ÙØ°Ù Ø§ÙØ¯Ùا٠تسÙ
٠بÙstatic.
Ù٠اÙÙ class, ÙØªÙ
Ø¥ÙØØ§ÙÙÙ
بÙÙÙ
Ø© Ø±Ø¦ÙØ³ÙØ© staticââ Ø Ù
Ø«Ù ÙØ°Ø§:
class User {
static staticMethod() {
alert(this === User);
}
}
User.staticMethod(); // true
ÙØ°Ø§ Ù٠اÙÙØ§Ùع ÙÙØ¹Ù ÙÙØ³ Ø§ÙØ´ÙØ¡ Ø¹ÙØ¯ تعÙÙÙÙ ÙØ®Ø§ØµÙØ© ٠باشرة:
class User {}
User.staticMethod = function () {
alert(this === User);
};
User.staticMethod(); // true
ÙÙÙ
Ø© this ÙÙUser.staticMethod ()ÙÙ Ù
ÙÙØ´Ø¦ اÙÙØ¦Ø©Ø§ÙÙ
ستخدÙ
ÙÙØ³Ù (ÙØ§Ø¹Ø¯Ø© âobject ÙØ¨Ù اÙÙÙØ·Ø©â).
عادة Ø ÙØªÙ Ø§Ø³ØªØ®Ø¯Ø§Ù Ø§ÙØ£Ø³Ø§ÙÙØ¨ Ø§ÙØ«Ø§Ø¨ØªØ© ÙØªÙÙÙØ° اÙÙØ¸Ø§Ø¦Ù Ø§ÙØªÙ ØªÙØªÙ ٠إÙ٠اÙÙØ¦Ø© Ø ÙÙÙÙ ÙÙØ³ ÙØ£Ù object ٠عÙÙ Ù ÙÙØ§.
عÙ٠سبÙ٠اÙÙ
Ø«Ø§Ù Ø ÙØ¯ÙÙØ§ objects Article ÙÙØØªØ§Ø¬ Ø¥ÙÙ ÙØ¸ÙÙØ© ÙÙ
ÙØ§Ø±ÙØªÙØ§. Ø§ÙØÙ Ø§ÙØ·Ø¨Ùع٠ÙÙ Ø¥Ø¶Ø§ÙØ© طرÙÙØ© Article.compare Ø Ø¹Ù٠اÙÙØÙ Ø§ÙØªØ§ÙÙ:
class Article {
constructor(title, date) {
this.title = title;
this.date = date;
}
static compare(articleA, articleB) {
return articleA.date - articleB.date;
}
}
// usage
let articles = [
new Article("HTML", new Date(2019, 1, 1)),
new Article("CSS", new Date(2019, 0, 1)),
new Article("JavaScript", new Date(2019, 11, 1))
];
articles.sort(Article.compare);
alert( articles[0].title ); // CSS
ÙÙØ§ âArticle.compareâ ØªÙ٠اÙÙ
ÙØ§Ùات âØ£Ø¹ÙØ§Ùâ Ø ÙÙØ³ÙÙØ© ÙÙ
ÙØ§Ø±ÙØªÙØ§. Ø¥ÙÙØ§ ÙÙØ³Øª Ø¯Ø§ÙØ© ÙÙ article Ø ÙÙÙÙ Ø¨Ø¯ÙØ§Ù Ù
٠اÙÙ class بأÙÙ
ÙÙ.
٠ثا٠آخر Ù٠٠ا ÙØ³Ù ٠طرÙÙØ© âØ§ÙÙ ØµÙØ¹â. تخÙÙ Ø ÙØÙ Ø¨ØØ§Ø¬Ø© Ø¥Ù٠طر٠ÙÙÙÙØ© ÙØ¥Ùشاء Ù ÙØ§Ù:
- Ø¥ÙØ´Ø§Ø¡ Ø¨ÙØ§Ø³Ø·Ø© Ù
عÙÙ
ات Ù
عÙÙØ© (
Ø§ÙØ¹ÙÙØ§ÙØØ§ÙØªØ§Ø±ÙØ®Ù٠ا Ø¥Ù٠ذÙÙ). - Ø¥ÙØ´Ø§Ø¡ Ù ÙØ§Ù ÙØ§Ø±Øº Ø¨ØªØ§Ø±ÙØ® اÙÙÙÙ .
- ⦠أ٠بطرÙÙØ© أخرÙ.
ÙÙ Ù٠تÙÙÙØ° Ø§ÙØ·Ø±ÙÙØ© Ø§ÙØ£ÙÙÙ Ù Ù ÙØ¨Ù اÙÙ ÙØ´Ø¦. ÙÙÙØ«Ø§ÙÙ ÙÙ ÙÙÙØ§ ع٠٠طرÙÙØ© ثابتة ÙÙÙØ¦Ø©.
Ù
ث٠Article.createTodays() ÙÙØ§:
class Article {
constructor(title, date) {
this.title = title;
this.date = date;
}
static createTodays() {
// remember, this = Article
return new this("Today's digest", new Date());
}
}
let article = Article.createTodays();
alert( article.title ); // Today's digest
Ø§ÙØ¢Ù ÙÙ Ù٠٠رة ÙØØªØ§Ø¬ ÙÙÙØ§ Ø¥ÙÙ Ø¥ÙØ´Ø§Ø¡ Ù ÙØ®Øµ اÙÙÙÙ Ø ÙÙ ÙÙÙØ§ استدعاء ``Article.createTodays ()`. ٠رة Ø£Ø®Ø±Ù Ø ÙØ°Ù ÙÙØ³Øª طرÙÙØ© ÙÙ ÙØ§ÙØ© Ø ÙÙÙÙÙØ§ طرÙÙØ© ÙÙÙØµÙ بأÙÙ ÙÙ.
ÙØªÙ Ø§Ø³ØªØ®Ø¯Ø§Ù Ø§ÙØ£Ø³Ø§ÙÙØ¨ Ø§ÙØ«Ø§Ø¨ØªØ© Ø£ÙØ¶Ùا Ù٠اÙÙØ¦Ø§Øª اÙ٠تعÙÙØ© Ø¨ÙØ§Ø¹Ø¯Ø© Ø§ÙØ¨ÙØ§ÙØ§Øª ÙÙØ¨ØØ« / ØÙظ / Ø¥Ø²Ø§ÙØ© Ø§ÙØ¥Ø¯Ø®Ø§Ùات Ù Ù ÙØ§Ø¹Ø¯Ø© Ø§ÙØ¨ÙØ§ÙØ§Øª Ø Ù Ø«Ù ÙØ°Ø§:
// assuming Article is a special class for managing articles
// static method to remove the article:
Article.remove({ id: 12345 });
Static properties
Ø§ÙØ®ØµØ§Ø¦Øµ Ø§ÙØ«Ø§Ø¨ØªØ© Ù
Ù
ÙÙØ© Ø£ÙØ¶Ùا Ø ÙÙ٠تبد٠Ù
ث٠خصائص اÙÙØ¦Ø© Ø§ÙØ¹Ø§Ø¯ÙØ© Ø ÙÙÙÙ ÙØªÙ
Ø¥ÙØØ§ÙÙØ§ ب٠static:
class Article {
static publisher = 'Ilya Kantor';
}
alert(Article.publisher); // Ilya Kantor
That is the same as a direct assignment to Article:
Article.publisher = 'Ilya Kantor';
Inheritance of static properties and methods
Ø§ÙØ®ØµØ§Ø¦Øµ ÙØ§ÙأساÙÙØ¨ Ø§ÙØ«Ø§Ø¨ØªØ© Ù ÙØ±Ùثة.
عÙ٠سبÙ٠اÙÙ
Ø«Ø§Ù Ø Animal.compare ÙAnimal.planet ÙÙ Ø§ÙØ´Ùرة Ø£Ø¯ÙØ§Ù Ù
ÙØ±Ùثة ÙÙÙ
Ù٠اÙÙØµÙ٠إÙÙÙØ§ باسÙ
Rabbit.compare ÙRabbit.planet:
class Animal {
static planet = "Earth";
constructor(name, speed) {
this.speed = speed;
this.name = name;
}
run(speed = 0) {
this.speed += speed;
alert(`${this.name} runs with speed ${this.speed}.`);
}
static compare(animalA, animalB) {
return animalA.speed - animalB.speed;
}
}
// Inherit from Animal
class Rabbit extends Animal {
hide() {
alert(`${this.name} hides!`);
}
}
let rabbits = [
new Rabbit("White Rabbit", 10),
new Rabbit("Black Rabbit", 5)
];
rabbits.sort(Rabbit.compare);
rabbits[0].run(); // Black Rabbit runs with speed 5.
alert(Rabbit.planet); // Earth
Ø§ÙØ¢Ù Ø¹ÙØ¯Ù ا ÙØ³Ù Ù âRabbit.compareâ Ø Ø³ÙØªÙ استدعاء âAnimal.compareâ Ø§ÙÙ ÙØ±ÙØ«.
ÙÙÙ ÙØ¹Ù ÙØ ٠رة Ø£Ø®Ø±Ù Ø Ø¨Ø§Ø³ØªØ®Ø¯Ø§Ù Ø§ÙÙ٠اذج Ø§ÙØ£ÙÙÙØ©. Ù٠ا ÙÙØª ÙØ¯ Ø®Ù ÙØª باÙÙØ¹Ù Ø ÙØ¥Ù ÙÙÙ Ø© âÙÙ ØªØ¯â ØªØ¹Ø·Ù ÙÙÙ Ø© âØ£Ø±ÙØ¨â ÙØ´Ùر "[[ÙÙ ÙØ°Ø¬ Ø£ÙÙÙ]]` Ø¥ÙÙ âØÙÙØ§Ùâ.
So, Rabbit extends Animal creates two [[Prototype]] references:
Rabbitfunction prototypally inherits fromAnimalfunction.Rabbit.prototypeprototypally inherits fromAnimal.prototype.
As a result, inheritance works both for regular and static methods.
Here, letâs check that by code:
class Animal {}
class Rabbit extends Animal {}
// for statics
alert(Rabbit.__proto__ === Animal); // true
// for regular methods
alert(Rabbit.prototype.__proto__ === Animal.prototype); // true
Ù ÙØ®Øµ
ÙØªÙ Ø§Ø³ØªØ®Ø¯Ø§Ù Ø§ÙØ£Ø³Ø§ÙÙØ¨ Ø§ÙØ«Ø§Ø¨ØªØ© ÙÙÙØ¸ÙÙØ© Ø§ÙØªÙ ØªÙØªÙ ٠إÙ٠اÙÙØ¦Ø© âÙÙÙâ. ÙØ§ ÙØªØ¹ÙÙ Ø§ÙØ£Ù ر ب٠ثÙÙ ÙØ¦Ø© Ù ÙÙ ÙØ³Ø©.
عÙ٠سبÙ٠اÙÙ
Ø«Ø§Ù Ø Ø·Ø±ÙÙØ© ÙÙÙ
ÙØ§Ø±ÙØ© Article.compare (article1 Ø article2) أ٠طرÙÙØ© Ù
ØµÙØ¹ Article.createTodays ().
ÙØªÙ ØªØ³Ù ÙØªÙا بÙÙÙ Ø© âØ«Ø§Ø¨Øªâ ÙÙ Ø¥Ø¹ÙØ§Ù اÙÙØ¦Ø©.
ÙØªÙ Ø§Ø³ØªØ®Ø¯Ø§Ù Ø§ÙØ®ØµØ§Ø¦Øµ Ø§ÙØ«Ø§Ø¨ØªØ© Ø¹ÙØ¯Ù ا ÙØ±ØºØ¨ Ù٠تخزÙÙ Ø§ÙØ¨ÙØ§ÙØ§Øª عÙ٠٠ستÙ٠اÙÙØµÙ Ø§ÙØ¯Ø±Ø§Ø³Ù Ø ÙØ§ÙØªÙ ÙØ§ ترتبط Ø£ÙØ¶Ùا ب٠ثÙÙ.
Ø§ÙØµÙغة ÙÙ:
class MyClass {
static property = ...;
static method() {
...
}
}
Ù
٠اÙÙØ§ØÙØ© اÙÙÙÙØ© Ø ÙØ¥Ù Ø§ÙØ¥Ø¹ÙØ§Ù Ø§ÙØ«Ø§Ø¨Øª ÙÙ ÙÙØ³Ù Ø§ÙØªØ®ØµÙص ÙÙÙ class ÙÙØ³Ùا:
MyClass.property = ...
MyClass.method = ...
Ø§ÙØ®ØµØ§Ø¦Øµ ÙØ§ÙأساÙÙØ¨ Ø§ÙØ«Ø§Ø¨ØªØ© Ù ÙØ±Ùثة.
باÙÙØ³Ø¨Ø© Ø¥ÙÙ âØ§ÙÙØ¦Ø© B Ø§ÙØªÙ تÙ
د Aâ Ø ÙØ´Ùر اÙÙÙ
ÙØ°Ø¬ Ø§ÙØ£ÙÙÙ ÙÙÙØ¦Ø© B ÙÙØ³Ùا Ø¥ÙÙA: B. [[Prototype]] = A. ÙØ°Ø§ Ø Ø¥Ø°Ø§ ÙÙ
ÙØªÙ
Ø§ÙØ¹Ø«Ùر عÙÙ ØÙÙ ÙÙ B Ø ÙØ³ÙستÙ
ر Ø§ÙØ¨ØØ« ÙÙA.
Ø§ÙØªØ¹ÙÙÙØ§Øª
<code>Ø ÙÙÙÙØ«Ùر Ù Ù Ø§ÙØ³Ø·Ùر استخدÙ<pre>Ø ÙÙØ£Ùثر Ù Ù 10 Ø³Ø·ÙØ± استخد٠(plnkr, JSBin, codepenâ¦)