You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `"prototype"`property is widely used by the core of JavaScript itself. All built-in constructor functions use it.
3
+
إن الخاصية `"prototype"`مستخدمة بشكل واسع من الجافاسكريبت نفسها، حيث أن كل الدوال البانية (constructor functions) تستخدمها.
4
4
5
-
First we'll see at the details, and then how to use it for adding new capabilities to built-in objects.
5
+
أولًا سنرى التفاصيل، ثم نتعلم كيف نستخدمها لإضافة إمكانيات جديدة للكائنات الموجودة بالفعل (built-in objects).
6
6
7
7
## Object.prototype
8
8
9
-
Let's say we output an empty object:
9
+
دعنا نقول أننا سنطبع كائنًا فارغًا:
10
10
11
11
```js run
12
12
let obj = {};
13
13
alert(obj); // "[object Object]" ?
14
14
```
15
15
16
-
Where's the code that generates the string`"[object Object]"`? That's a built-in `toString`method, but where is it? The `obj`is empty!
16
+
أين الكود المسؤول عن التحويل إلى النص`"[object Object]"`؟ إنها الدالة `toString`الموجودة بالفعل ، ولكن أين هى؟ الكائن `obj`فارغ !
17
17
18
-
...But the short notation `obj = {}`is the same as`obj = new Object()`, where `Object`is a built-in object constructor function, with its own `prototype`referencing a huge object with `toString`and other methods.
18
+
...ولكن الصيغة `obj = {}`هي نفسها هذه الصيغة`obj = new Object()`، حيث أن `Object`هو دالة بانية للكائنات موجودة بالفعل والتى تحتوى على الخاصية `prototype`التى تحتوى على مرجع لكائن ضخم يحتوى على الدالة `toString`ودوال أخري.
19
19
20
-
Here's what's going on:
20
+
هاهنا ما يحدث:
21
21
22
22

23
23
24
-
When `new Object()`is called (or a literal object`{...}` is created), the `[[Prototype]]`of it is set to `Object.prototype`according to the rule that we discussed in the previous chapter:
24
+
عندما يتم استدعاء `new Object()`(أو إنشاء الكائن العادى`{...}`)، ستكون قيمة `[[Prototype]]`للكائن الناتج تشير إلى `Object.prototype`طبقًا للقاعدة التى ناقشناها فى الفصل السابق:
25
25
26
26

27
27
28
-
So then when `obj.toString()`is called the method is taken from`Object.prototype`.
28
+
لذلك عندما يتم استدعاء `obj.toString()`فإن الدالة مأخوذة من`Object.prototype`.
Please note that there is no more `[[Prototype]]`in the chain above`Object.prototype`:
39
+
لاحظ أنه لا يوجد المزيد من `[[Prototype]]`فى السلسة فوق`Object.prototype`:
40
40
41
41
```js run
42
42
alert(Object.prototype.__proto__); // null
43
43
```
44
44
45
-
## Other built-in prototypes
45
+
## نماذج أخرى موجودة بالفعل (built-in prototypes)
46
46
47
-
Other built-in objects such as `Array`, `Date`, `Function`and others also keep methods in prototypes.
47
+
إن الكائنات الأخرى الموجوده بالفعل مثل `Array`, `Date`, `Function`وغيرهم يحتفظون بدوال فى النماذج (prototypes).
48
48
49
-
For instance, when we create an array`[1, 2, 3]`, the default `new Array()`constructor is used internally. So `Array.prototype`becomes its prototype and provides methods. That's very memory-efficient.
49
+
علي سبيل المثال، عندما نقوم بإنشاء قائمة`[1, 2, 3]`، فإن الدالة البانية الموجودة بالفعل `new Array()`يتم استخدامها داخليًا. ولذلك تصبح `Array.prototype`النموذج الخاص بها وتمنحها دوال خاصة وهذا شيئ جيد جدًّا للذاكرة.
50
50
51
-
By specification, all of the built-in prototypes have `Object.prototype`on the top. That's why some people say that "everything inherits from objects".
51
+
كما ذُكر فى المصدر، فإن كل النماذج (prototypes) الموجودة بالفعل لديها `Object.prototype`على القمة فى الأعلى. وهذا مايدفع بعض الأشخاص للقول بأن "كل شيئ يرث من الكائنات".
52
52
53
-
Here's the overall picture (for 3 built-ins to fit):
Some methods in prototypes may overlap, for instance, `Array.prototype`has its own `toString`that lists comma-delimited elements:
72
+
بعض الدوال فى النماذج يمكن أن تتداخل، فعلى سبيل المثال، تملك `Array.prototype`الدالة `toString`الخاصة بها والتى تقوم بإرجاع نص يحوى عناصر القائمة وبينها الفاصلة:
73
73
74
74
```js run
75
75
let arr = [1, 2, 3];
76
-
alert(arr); // 1,2,3 <-- the result of Array.prototype.toString
76
+
alert(arr); // 1,2,3 <-- نتيجة Array.prototype.toString
77
77
```
78
78
79
-
As we've seen before,`Object.prototype`has `toString`as well, but `Array.prototype`is closer in the chain, so the array variant is used.
79
+
كما رأينا سابقًا، تملك`Object.prototype`أيضًا الدالة `toString`ولكن `Array.prototype`هي الأقرب فى السلسلة ولذلك يتم استخدام الدالة الخاصة بالقائمة.
80
80
81
81

82
82
83
-
In-browser tools like Chrome developer console also show inheritance (`console.dir`may need to be used for built-in objects):
83
+
تعرض الأدوات الموجودة فى المتصفح أيضًا الوراثة (يمكن أن يتم استخدام `console.dir`مع بعض الكائنات الموجودة بالفعل ):
84
84
85
85

86
86
87
-
Other built-in objects also work the same way. Even functions -- they are objects of a built-in `Function`constructor, and their methods (`call`/`apply`and others) are taken from `Function.prototype`. Functions have their own`toString` too.
87
+
تعمل الكائنات الموجودة بالفعل الأخرى بنفس الطريقة. حتى الدوال -- هي عبارة عن كائنات مبنية عن طريق الدالة البانية `Function`والدوال الخاصه بها (مثل `call`/`apply`وغيرها) مأخوذة من `Function.prototype`. وتحتوى الدوال على الدالة`toString`الخاصة بها أيضًا.
88
88
89
89
```js run
90
90
functionf() {}
91
91
92
92
alert(f.__proto__==Function.prototype); // true
93
-
alert(f.__proto__.__proto__==Object.prototype); // true, inherit from objects
93
+
alert(f.__proto__.__proto__==Object.prototype); // true, ترث من الكائنات
94
94
```
95
95
96
-
## Primitives
96
+
## القيم المفردة Primitives
97
97
98
-
The most intricate thing happens with strings, numbers and booleans.
98
+
أكثر الأشياء المعقدة تحدث مع النصوص والأرقام والقيم المطلقة.
99
99
100
-
As we remember, they are not objects. But if we try to access their properties, temporary wrapper objects are created using built-in constructors `String`, `Number`and`Boolean`. They provide the methods and disappear.
100
+
كما نتذكر فإنهم ليسو عبارة عن كائنات، ولكن إذا حاولنا أن نصل إلى خصائصهم فسيتم إحاطتها بكائن باستخدام الدوال البانية `String` و `Number`و`Boolean`، حيث يمنحونهم الدوال ثم يختفون.
101
101
102
-
These objects are created invisibly to us and most engines optimize them out, but the specification describes it exactly this way. Methods of these objects also reside in prototypes, available as`String.prototype`, `Number.prototype`and`Boolean.prototype`.
102
+
هذه الكائنات تم إنشاؤها لنا خفيةً وأغلب المحركات تقوم بتحسين ذلك، ولكن يصفها المصدر بهذه الطريقة بالضبط. ودوال هذه الكائنات توجد أيضًا فى النماذج وتكون متاحة كـ`String.prototype` و `Number.prototype`و`Boolean.prototype`.
103
103
104
-
```warn header="Values`null`and`undefined`have no object wrappers"
105
-
Special values`null`and`undefined`stand apart. They have no object wrappers, so methods and properties are not available for them. And there are no corresponding prototypes either.
104
+
```warn header="القيم`null`و`undefined`ليس لها كائنات حاوية"
105
+
القيم الخاصة`null`و`undefined`تقف بعيدًا عن هذا. حيث أنهم ليس لديهم كائنات حاوية (object wrappers)، ولذلك فإن الدوال والخصائص غير متاحة لهم وليس هناك نماذج لهم.
During the process of development, we may have ideas for new built-in methods we'd like to have, and we may be tempted to add them to native prototypes. But that is generally a bad idea.
121
+
وخلال التطبيق العملى يمكن أن تخطر لنا أفكار لدوال أخرى نريد أن ننشئها ويمكننا إضافتها للنماذج، ولكن هذا يُعد فكرة سيئة بشكل عام.
122
122
123
123
```warn
124
-
Prototypes are global, so it's easy to get a conflict. If two libraries add a method `String.prototype.show`, then one of them will be overwriting the method of the other.
124
+
إن النماذج متاحة بشكل عام، ولذلك فإنه من السهل أن يحدث تعارض. فإذا كان هناك مكتبتان أضافتا نفس الدالة `String.prototype.show`، إذن فإن واحدة منهن ستستبدل عمل الأخرى.
125
125
126
-
So, generally, modifying a native prototype is considered a bad idea.
126
+
ولذلك، بشكل عام، فإن تعديل النماذج البدائية يُعد فكرة سيئة.
127
127
```
128
128
129
-
**In modern programming, there is only one case where modifying native prototypes is approved. That's polyfilling.**
129
+
**فى لغات البرمجة الحديثة، توجد حالة واحدة لتعديل النماذج البدائية. وهي تعدد الأشكال polyfilling**
130
130
131
-
Polyfilling is a term for making a substitute for a method that exists in the JavaScript specification, but is not yet supported by a particular JavaScript engine.
131
+
تعدد الأشكال هو مصطلح يعني إنشاء نسخه من دالة موجودة فى مصدر الجافاسكريبت ولكنها غير مدعومة بعد من محرك جافاسكريبت معين.
132
132
133
-
We may then implement it manually and populate the built-in prototype with it.
133
+
يمكننا إذن كتابتها يدويّاً وإضافتها للنموذج.
134
134
135
-
For instance:
135
+
على سبيل المثال:
136
136
137
137
```js run
138
138
if (!String.prototype.repeat) {
139
-
//if there's no such method
140
-
//add it to the prototype
139
+
//إذا لم توجد هذه الدالة
140
+
//أضفها للنموذج
141
141
142
142
String.prototype.repeat=function (n) {
143
-
//repeat the string n times
143
+
//كرر النص n من المرات
144
144
145
-
//actually, the code should be a little bit more complex than that
145
+
//فى الحقيقة، يجب أن يكون الكود أكثر تعقيدًا بقليل من هذا
146
146
// (the full algorithm is in the specification)
147
-
//but even an imperfect polyfill is often considered good enough
147
+
//ولكن حتي تعدد الأشكال الغير كامل غالبًا ما يكون كافيًا
148
148
returnnewArray(n +1).join(this);
149
149
};
150
150
}
151
151
152
152
alert("La".repeat(3)); // LaLaLa
153
153
```
154
154
155
-
## Borrowing from prototypes
155
+
## الإستعارة من النماذج
156
156
157
-
In the chapter <info:call-apply-decorators#method-borrowing>we talked about method borrowing.
157
+
فى فصل <info:call-apply-decorators#method-borrowing>تحدثنا عن استعارة الدوال.
158
158
159
-
That's when we take a method from one object and copy it into another.
159
+
وهذا يكون عندما نأخذ دالةً ما من كائن وننسخها لكائن آخر.
160
160
161
-
Some methods of native prototypes are often borrowed.
161
+
بعض الدوال غالبًا ما يتم استعارتها من النماذج البدائية.
162
162
163
-
For instance, if we're making an array-like object, we may want to copy some `Array`methods to it.
163
+
على سبيل المثال، إذا كنا ننشئ كائنًا شبيهًا بالقائمة، فإننا يمكن أن نريد أن ننسخ بعذ دوال الكائن `Array`إليه.
It works because the internal algorithm of the built-in `join`method only cares about the correct indexes and the `length` property. It doesn't check if the object is indeed an array. Many built-in methods are like that.
181
+
هذا يعمل لأن الدالة الموجودة بالفعل `join`تهتم فقط بالأرقام الصحيحه و الخاصية `length`، ولا تفحص إذا كان الكائن هو قائمة بالفعل. والكثير من الدوال تعمل بنفس الطريقة.
182
182
183
-
Another possibility is to inherit by setting `obj.__proto__`to `Array.prototype`, so all `Array`methods are automatically available in`obj`.
183
+
وهناك إمكانية أخرى وهي الوراثة وذلك عن طريق أن نجعل `obj.__proto__`تشير إلى `Array.prototype` ولذلك فإن كل دوال الكائن `Array`ستكون متاحة تلقائيًا للكائن`obj`.
184
184
185
-
But that's impossible if `obj`already inherits from another object. Remember, we only can inherit from one object at a time.
185
+
ولكن هذا مستحيل إذا كان الكائن `obj`يرث بالفعل من كائن آخر، تذكر أننا يمكننا أن نرث من كائن واحد فقط فى المرة الواحدة.
186
186
187
-
Borrowing methods is flexible, it allows to mix functionalities from different objects if needed.
187
+
إن استعارة الدوال مرن ويسمح بمزج الوظائف من كائنات مختلفة إذا أردنا ذلك.
188
188
189
-
## Summary
189
+
## الملخص
190
190
191
-
-All built-in objects follow the same pattern:
192
-
-The methods are stored in the prototype (`Array.prototype`, `Object.prototype`, `Date.prototype`, etc.)
193
-
-The object itself stores only the data (array items, object properties, the date)
194
-
- Primitives also store methods in prototypes of wrapper objects: `Number.prototype`, `String.prototype`and`Boolean.prototype`. Only `undefined`and`null` do not have wrapper objects
195
-
- Built-in prototypes can be modified or populated with new methods. But it's not recommended to change them. The only allowable case is probably when we add-in a new standard, but it's not yet supported by the JavaScript engine
191
+
-تتبع كل الكائنات الموجودة بالفعل نفس النمط:
192
+
-يتم الإحتفاظ بالدوال فى النموذج (prototype) (`Array.prototype`, `Object.prototype`, `Date.prototype`, إلخ.)
193
+
-يتم تخزين البيانات فقط فى الكائن (عناصر قائمة أو خصائص كائن أو تاريخ)
194
+
-القيم المفردة (Primitives) تخزن الدوال فى نموذج خاص بالكائن الحاوي (wrapper object): `Number.prototype` و `String.prototype`و`Boolean.prototype`. ولا يوجد كائن حاوى للقيمتين `undefined`و`null`.
195
+
-النماذج الموجودة بالفعل (Built-in prototypes) يمكن تعديلها أو إضافة دوال جديدة لها، ولكن هذا غير موصيً به، وإن الحالة الوحيدة المسموح فيها بذلك هي عندما نريد أن نذيف وظيفة جديدة موجودة فى المصدر ولكنها مازالت غير مدعومة من محرك جافاسكريبت معين.
0 commit comments