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
Until now, a property was a simple "key-value" pair to us. But an object property is actually a more flexible and powerful thing.
6
+
حتى الآن, الخاصيه كانت لنا زوجاً بسيطاً من "المفاتيح-القيم". و لكن خاصية الكائن هى حقاً اكثر مرونة و قوة.
7
7
8
-
In this chapter we'll study additional configuration options, and in the next we'll see how to invisibly turn them into getter/setter functions.
8
+
فى هذا القسم سوف ندرس خصائص ضبط إضافية, وفي الفصل الّذي يليه سنرى كيف نحوّلها إلى دوال جلب/ضبط (Setters/Getters) أيضًا.
9
9
10
-
## Property flags
10
+
## رايات الخصائص
11
11
12
-
Object properties, besides a**`value`**, have three special attributes (so-called "flags"):
12
+
خصائص الكائنات, بالإضافة الى**`قيمتها`**, لديها ثلاث سمات مميزة اخرى (لذلك تسمى "flags" او رايات) :
13
13
14
-
-**`writable`** -- if `true`, the value can be changed, otherwise it's read-only.
15
-
-**`enumerable`** -- if `true`, then listed in loops, otherwise not listed.
16
-
-**`configurable`** -- if `true`, the property can be deleted and these attributes can be modified, otherwise not.
14
+
-**`writable` : قابلة التعديل** -- إذا كانت `true`, يمكن تغيير القيمة, غير ذلك فالقيمة للقراءة فقط.
15
+
-**`enumerable` : قابلة الإحصاء** -- إذا كانت `true`, سوف يظهر مفتاح الخاصية ضمن مفاتيح الكائن عند إستخدام **`for..in`**, غير ذلك فلن يظهر.
16
+
-**`configurable` : قابلة إعادة الضبط** -- إذا كانت `true`, فيمكن حذف الخاصية وتعديل هذه السمات, غير ذلك فلا.
17
17
18
-
We didn't see them yet, because generally they do not show up. When we create a property "the usual way", all of them are `true`. But we also can change them anytime.
18
+
لم نري تلك الرايات ختي الآن, لأنهم بشكل عام لا يظهرون. عندما نقوم بعمل خاصية "بالطريقة العادية", فكل هذه السمات تكون بقيمة `true`. و لكن يمكننا طبعاً تغييرها متى أردنا.
19
19
20
-
First, let's see how to get those flags.
20
+
اولاً, دعنا نري كيف يمكننا الحصول علي تلك الرايات.
21
21
22
-
The method [Object.getOwnPropertyDescriptor](mdn:js/Object/getOwnPropertyDescriptor)allows to query the *full* information about a property.
22
+
الطريقة [Object.getOwnPropertyDescriptor](mdn:js/Object/getOwnPropertyDescriptor)تسمح بالإستعلام *الكامل* عن المعلومات الخاصة بأيّ خاصية.
23
23
24
-
The syntax is:
24
+
و صياغتها تكون كالآتي:
25
25
```js
26
26
let descriptor =Object.getOwnPropertyDescriptor(obj, propertyName);
27
27
```
28
28
29
29
`obj`
30
-
: The object to get information from.
30
+
: الكائن الّذي سنجلب معلوماته.
31
31
32
32
`propertyName`
33
-
: The name of the property.
33
+
: اسم الخاصية الّتي نريدها.
34
34
35
-
The returned value is a so-called "property descriptor" object: it contains the value and all the flags.
35
+
القيمة العائدة تسمي بكائن "واصف الخصائص" : و هي تحتوي علي القيمه و جميع الرايات.
36
36
37
-
For instance:
37
+
اليك مثالاً:
38
38
39
39
```js run
40
40
let user = {
@@ -44,7 +44,7 @@ let user = {
44
44
let descriptor =Object.getOwnPropertyDescriptor(user, 'name');
: The object and its property to apply the descriptor.
66
+
: الكائن الّذي سنطبّق عليه الواصِف، واسم الخاصية.
67
67
68
68
`descriptor`
69
-
: Property descriptor object to apply.
69
+
: واصِف الخصائص الّذي سنطبّقه على الكائن.
70
70
71
-
If the property exists, `defineProperty`updates its flags. Otherwise, it creates the property with the given value and flags; in that case, if a flag is not supplied, it is assumed`false`.
71
+
لو كانت الخاصية موجوده, `defineProperty`سوف تقوم بتحديث راياتها. غير ذلك, وإلّا فسيُنشئ الخاصية بهذه القيمة الممرّرة والرايات كذلك; في هذه الحالة, إذا كانت الراية غير موجوده, سوف يُفترض قيمتها بـ`false`.
72
72
73
-
For instance, here a property `name`is created with all falsy flags:
73
+
إليك مثالاً, هنا الخاصية `name`سوف يتم إنشائها حيث تكون كل راياتها تساوى **`false`**:
Compare it with "normally created" `user.name`above: now all flags are falsy. If that's not what we want then we'd better set them to `true`in`descriptor`.
99
+
قارن ذلك مع `user.name`"التي انشأناها بشكل طبيعي" بالإعلي: الآن كل الرايات لديها القيمة `false`. إذا لم يكن هذا ما نريدة إذا سوف يكون من الأفضل ضبط قيمتهم بـ `true`في`descriptor`.
100
100
101
-
Now let's see effects of the flags by example.
101
+
نرى الآن تأثيرات هذه الرايات في هذا المثال.
102
102
103
-
## Non-writable
103
+
## منع قابلية التعديل
104
104
105
-
Let's make `user.name`non-writable (can't be reassigned) by changing `writable`flag:
105
+
لنجعل `user.name`غير قابلة للتعديل (لا يمكن إسناد قيمة لها) عن طريق تغيير قيمة الراية `writable` :
user.name="Pete"; //Error: Cannot assign to read only property 'name'
119
+
user.name="Pete"; //خطأ: لا يمكن إسناد القيم إلى الخاصية `name` إذ هي للقراءة فقط
120
120
*/!*
121
121
```
122
122
123
-
Now no one can change the name of our user, unless they apply their own `defineProperty`to override ours.
123
+
الآن يستحيل على أيّ شخص تعديل اسم هذا المستخدم, إلا عند تطبيق `defineProperty`لتعديل ما فعلناه نحن.
124
124
125
-
```smart header="Errors appear only in strict mode"
126
-
In the non-strict mode, no errors occur when writing to non-writable properties and such. But the operation still won't succeed. Flag-violating actions are just silently ignored in non-strict.
125
+
```smart header="لا تظهر الأخطاء إلّا في الوضع الصارم `strict mode`"
126
+
في الوضع الغير صارم `non-strict mode`, لا يحدث أخطاء عند التعديل علي خاصية غير قابلة للتعديل. و لكن العمليه لن تتم بنجاح أيضاً. أخطاء خرق الرايه يتم تجاهلها بصمت في الوضع الغير صارم `non-strict`.
127
127
```
128
128
129
-
Here's the same example, but the property is created from scratch:
129
+
إليك نفس المثال, و لكن سوف يتم إنشاء الخاصية من الصفر:
130
130
131
131
```js run
132
132
let user = { };
133
133
134
134
Object.defineProperty(user, "name", {
135
135
*!*
136
136
value: "John",
137
-
//for new properties we need to explicitly list what's true
137
+
// لو كانت الخصائص جديدة فعلينا إسناد قيمها إسنادًا صريحًا
138
138
enumerable: true,
139
139
configurable: true
140
140
*/!*
@@ -144,11 +144,11 @@ alert(user.name); // John
144
144
user.name = "Pete"; // Error
145
145
```
146
146
147
-
## Non-enumerable
147
+
## منع قابلية الإحصاء
148
148
149
-
Now let's add a custom `toString`to`user`.
149
+
الآن دعنا نضيف الطريقة `toString`الى الكائن`user`.
150
150
151
-
Normally, a built-in `toString`for objects is non-enumerable, it does not show up in`for..in`. But if we add a`toString`of our own, then by default it shows up in`for..in`, like this:
151
+
عادةً, لا يمكننا إستخدام `toString`مع الكائنات و ذلك لإنها غير قابلة للإحصاء, و هي لا تظهر عند إستخدام`for..in`. و لكن إذا قمنا بإضافة`toString`الخاصة بنا, إذا بشكل افتراضي سوف تظهر عند إستخدام`for..in`, كما فى المثال التالي:
152
152
153
153
```js run
154
154
let user = {
@@ -158,11 +158,11 @@ let user = {
158
158
}
159
159
};
160
160
161
-
//By default, both our properties are listed:
161
+
//بشكل إفتراضي, كلا الخاصيتين سوف يتم عرضهم:
162
162
for (let key in user) alert(key); // name, toString
163
163
```
164
164
165
-
If we don't like it, then we can set `enumerable:false`. Then it won't appear in a`for..in` loop, just like the built-in one:
165
+
لو لم نرد ذلك, يمكننا وضع `enumerable:false`. و سوف لن تظهر عند إستخدام`for..in`, كما فى الوضع العادى:
Together with`Object.defineProperties`it can be used as a "flags-aware" way of cloning an object:
287
+
بدمجه مع`Object.defineProperties`يمكن إستخدامها لنسخ الكائنات "ونحن على علمٍ براياتها":
288
288
289
289
```js
290
290
let clone =Object.defineProperties({}, Object.getOwnPropertyDescriptors(obj));
291
291
```
292
292
293
-
Normally when we clone an object, we use an assignment to copy properties, like this:
293
+
فعادةً حين ننسخ كائنًا, نستعمل الإسناد لنسخ الخصائص، هكذا:
294
294
295
295
```js
296
296
for (let key in user) {
297
297
clone[key] = user[key]
298
298
}
299
299
```
300
300
301
-
...But that does not copy flags. So if we want a "better" clone then`Object.defineProperties`is preferred.
301
+
...و لكن هذا لا ينسخ الرايات. لذا إذا كنا نريد نسخ "أفضل" سيكون إستخدام`Object.defineProperties`أفضل.
302
302
303
-
Another difference is that `for..in`ignores symbolic properties, but `Object.getOwnPropertyDescriptors`returns *all* property descriptors including symbolic ones.
303
+
إختلاف آخر و ذلك أن `for..in`تتجاهل الخصائص الرمزية (Symbolic Properties), و لكن `Object.getOwnPropertyDescriptors`تُعيد *كل* واصِفات الخصائص بما فيها الرمزية.
304
304
305
-
## Sealing an object globally
305
+
## إغلاق الكائنات على المستوى العام
306
306
307
-
Property descriptors work at the level of individual properties.
307
+
تعمل واصِفات الخصائص على مستوى الخصائص منفردةً. هناك أيضًا توابِع تقصر الوصول إلى الكائن كلّه.
308
308
309
-
There are also methods that limit access to the *whole* object:
0 commit comments