Skip to content

Commit 077d335

Browse files
committed
Property flags and descriptors
1 parent c896ea4 commit 077d335

1 file changed

Lines changed: 81 additions & 81 deletions

File tree

  • 1-js/07-object-properties/01-property-descriptors
Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11

2-
# Property flags and descriptors
2+
# رايات الخصائص و واصفاتها
33

4-
As we know, objects can store properties.
4+
كما نعلم, الكائنات يمكن ان تُخزن الخصائص.
55

6-
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+
حتى الآن, الخاصيه كانت لنا زوجاً بسيطاً من "المفاتيح-القيم". و لكن خاصية الكائن هى حقاً اكثر مرونة و قوة.
77

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) أيضًا.
99

10-
## Property flags
10+
## رايات الخصائص
1111

12-
Object properties, besides a **`value`**, have three special attributes (so-called "flags"):
12+
خصائص الكائنات, بالإضافة الى **`قيمتها`**, لديها ثلاث سمات مميزة اخرى (لذلك تسمى "flags" او رايات) :
1313

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`, فيمكن حذف الخاصية وتعديل هذه السمات, غير ذلك فلا.
1717

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`. و لكن يمكننا طبعاً تغييرها متى أردنا.
1919

20-
First, let's see how to get those flags.
20+
اولاً, دعنا نري كيف يمكننا الحصول علي تلك الرايات.
2121

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) تسمح بالإستعلام *الكامل* عن المعلومات الخاصة بأيّ خاصية.
2323

24-
The syntax is:
24+
و صياغتها تكون كالآتي:
2525
```js
2626
let descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);
2727
```
2828

2929
`obj`
30-
: The object to get information from.
30+
: الكائن الّذي سنجلب معلوماته.
3131

3232
`propertyName`
33-
: The name of the property.
33+
: اسم الخاصية الّتي نريدها.
3434

35-
The returned value is a so-called "property descriptor" object: it contains the value and all the flags.
35+
القيمة العائدة تسمي بكائن "واصف الخصائص" : و هي تحتوي علي القيمه و جميع الرايات.
3636

37-
For instance:
37+
اليك مثالاً:
3838

3939
```js run
4040
let user = {
@@ -44,7 +44,7 @@ let user = {
4444
let descriptor = Object.getOwnPropertyDescriptor(user, 'name');
4545

4646
alert( JSON.stringify(descriptor, null, 2 ) );
47-
/* property descriptor:
47+
/* واصف الخاصية:
4848
{
4949
"value": "John",
5050
"writable": true,
@@ -54,23 +54,23 @@ alert( JSON.stringify(descriptor, null, 2 ) );
5454
*/
5555
```
5656

57-
To change the flags, we can use [Object.defineProperty](mdn:js/Object/defineProperty).
57+
لتغيير الرايات, يمكننا إستخدام [Object.defineProperty](mdn:js/Object/defineProperty).
5858

59-
The syntax is:
59+
و صياغتها تكون كالآتي:
6060

6161
```js
6262
Object.defineProperty(obj, propertyName, descriptor)
6363
```
6464

6565
`obj`, `propertyName`
66-
: The object and its property to apply the descriptor.
66+
: الكائن الّذي سنطبّق عليه الواصِف، واسم الخاصية.
6767

6868
`descriptor`
69-
: Property descriptor object to apply.
69+
: واصِف الخصائص الّذي سنطبّقه على الكائن.
7070

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`.
7272

73-
For instance, here a property `name` is created with all falsy flags:
73+
إليك مثالاً, هنا الخاصية `name` سوف يتم إنشائها حيث تكون كل راياتها تساوى **`false`**:
7474

7575
```js run
7676
let user = {};
@@ -96,13 +96,13 @@ alert( JSON.stringify(descriptor, null, 2 ) );
9696
*/
9797
```
9898

99-
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`.
100100

101-
Now let's see effects of the flags by example.
101+
نرى الآن تأثيرات هذه الرايات في هذا المثال.
102102

103-
## Non-writable
103+
## منع قابلية التعديل
104104

105-
Let's make `user.name` non-writable (can't be reassigned) by changing `writable` flag:
105+
لنجعل `user.name` غير قابلة للتعديل (لا يمكن إسناد قيمة لها) عن طريق تغيير قيمة الراية `writable` :
106106

107107
```js run
108108
let user = {
@@ -116,25 +116,25 @@ Object.defineProperty(user, "name", {
116116
});
117117

118118
*!*
119-
user.name = "Pete"; // Error: Cannot assign to read only property 'name'
119+
user.name = "Pete"; // خطأ: لا يمكن إسناد القيم إلى الخاصية ‫ `name` إذ هي للقراءة فقط
120120
*/!*
121121
```
122122

123-
Now no one can change the name of our user, unless they apply their own `defineProperty` to override ours.
123+
الآن يستحيل على أيّ شخص تعديل اسم هذا المستخدم, إلا عند تطبيق `defineProperty` لتعديل ما فعلناه نحن.
124124

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`.
127127
```
128128
129-
Here's the same example, but the property is created from scratch:
129+
إليك نفس المثال, و لكن سوف يتم إنشاء الخاصية من الصفر:
130130
131131
```js run
132132
let user = { };
133133
134134
Object.defineProperty(user, "name", {
135135
*!*
136136
value: "John",
137-
// for new properties we need to explicitly list what's true
137+
// لو كانت الخصائص جديدة فعلينا إسناد قيمها إسنادًا صريحًا
138138
enumerable: true,
139139
configurable: true
140140
*/!*
@@ -144,11 +144,11 @@ alert(user.name); // John
144144
user.name = "Pete"; // Error
145145
```
146146

147-
## Non-enumerable
147+
## منع قابلية الإحصاء
148148

149-
Now let's add a custom `toString` to `user`.
149+
الآن دعنا نضيف الطريقة `toString` الى الكائن `user`.
150150

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`, كما فى المثال التالي:
152152

153153
```js run
154154
let user = {
@@ -158,11 +158,11 @@ let user = {
158158
}
159159
};
160160

161-
// By default, both our properties are listed:
161+
// بشكل إفتراضي, كلا الخاصيتين سوف يتم عرضهم:
162162
for (let key in user) alert(key); // name, toString
163163
```
164164

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`, كما فى الوضع العادى:
166166

167167
```js run
168168
let user = {
@@ -179,24 +179,24 @@ Object.defineProperty(user, "toString", {
179179
});
180180

181181
*!*
182-
// Now our toString disappears:
182+
// الآن toString اختفت:
183183
*/!*
184184
for (let key in user) alert(key); // name
185185
```
186186

187-
Non-enumerable properties are also excluded from `Object.keys`:
187+
الخصائص الغير قابلة للإحصاء يتم استثناءها من `Object.keys`:
188188

189189
```js
190190
alert(Object.keys(user)); // name
191191
```
192192

193-
## Non-configurable
193+
## منع قابلية إعادة الضبط
194194

195-
The non-configurable flag (`configurable:false`) is sometimes preset for built-in objects and properties.
195+
راية عدم الضبط (`configurable:false`) احياناً يتم إعدادها مسبقاً في بعض الكائنات والخصائص المضمّنة في اللغة.
196196

197-
A non-configurable property can not be deleted.
197+
الخاصية الغير قابلة للإحصاء لا يمكن حذفها.
198198

199-
For instance, `Math.PI` is non-writable, non-enumerable and non-configurable:
199+
فمثلاً, `Math.PI` غير قابلة للتعديل, غير قابلة للإحصاء و غير قابلة لإعادة الضبط:
200200

201201
```js run
202202
let descriptor = Object.getOwnPropertyDescriptor(Math, 'PI');
@@ -211,23 +211,23 @@ alert( JSON.stringify(descriptor, null, 2 ) );
211211
}
212212
*/
213213
```
214-
So, a programmer is unable to change the value of `Math.PI` or overwrite it.
214+
لذا, لن يستطيع المبرمج تغيير قيمة `Math.PI` أو التعديل عليها.
215215

216216
```js run
217-
Math.PI = 3; // Error
217+
Math.PI = 3; // خطأ
218218

219-
// delete Math.PI won't work either
219+
// delete Math.PI لن تعمل أيضًا
220220
```
221221

222-
Making a property non-configurable is a one-way road. We cannot change it back with `defineProperty`.
222+
إن تفعيل خاصيّة منع قابلية إعادة الضبط هو قرار لا عودة فيه. فلا يمكننا تغيير الراية (إتاحة قابلية إعادة الضبط) باستعمال `defineProperty`.
223223

224-
To be precise, non-configurability imposes several restrictions on `defineProperty`:
225-
1. Can't change `configurable` flag.
226-
2. Can't change `enumerable` flag.
227-
3. Can't change `writable: false` to `true` (the other way round works).
228-
4. Can't change `get/set` for an accessor property (but can assign them if absent).
224+
وللدقّة فهذا المنع يضع تقييدات أخرى على `defineProperty`:
225+
1. منع تغيير راية قابلية إعادة الضبط `configurable`.
226+
2. منع تغيير راية قابلية الإحصاء `enumerable`.
227+
3. منع تغيير راية قابلية التعديل `writable: false` الي `true` (و لكن العكس ممكن).
228+
4. منع تغيير ضابط وجالب واصف الوصول `get/set` (ولكن يمكن إسناد قيم إليه).
229229

230-
Here we are making `user.name` a "forever sealed" constant:
230+
هنا سوف نحدد الخاصية `user.name` ثابتة للأبد:
231231

232232
```js run
233233
let user = { };
@@ -239,26 +239,26 @@ Object.defineProperty(user, "name", {
239239
});
240240

241241
*!*
242-
// won't be able to change user.name or its flags
243-
// all this won't work:
242+
// لن يمكن تغيير user.name او الرايات الخاصه بها
243+
// كل ذلك لن يعمل:
244244
// user.name = "Pete"
245245
// delete user.name
246246
// defineProperty(user, "name", { value: "Pete" })
247-
Object.defineProperty(user, "name", {writable: true}); // Error
247+
Object.defineProperty(user, "name", {writable: true}); // خطأ
248248
*/!*
249249
```
250250

251-
```smart header="\"Non-configurable\" doesn't mean \"non-writable\""
252-
Notable exception: a value of non-configurable, but writable property can be changed.
251+
```smart header="\"منع قابلية إعادة الضبط\" لا يعني \"منع قابلية التعديل\""
252+
إستثناء ملحوظ: قيمة الخاصية التى لديها منع إعادة الضبط, و لكن لديها قابلية التعديل , تلك القيمة يمكن تغييرها.
253253
254-
The idea of `configurable: false` is to prevent changes to property flags and its deletion, not changes to its value.
254+
الفكره وراء `configurable: false` لمنع تغيير رايات الخاصية او حذفها, ليس لتغيير قيمتها.
255255
```
256256

257257
## Object.defineProperties
258258

259-
There's a method [Object.defineProperties(obj, descriptors)](mdn:js/Object/defineProperties) that allows to define many properties at once.
259+
يوجد طريقة [Object.defineProperties(obj, descriptors)](mdn:js/Object/defineProperties) و التي تسمح بتعريف كثير من الخصائص مره واحده.
260260

261-
The syntax is:
261+
و صياغتها تكون كالآتي:
262262

263263
```js
264264
Object.defineProperties(obj, {
@@ -268,7 +268,7 @@ Object.defineProperties(obj, {
268268
});
269269
```
270270

271-
For instance:
271+
مثال علي ذلك:
272272

273273
```js
274274
Object.defineProperties(user, {
@@ -278,54 +278,54 @@ Object.defineProperties(user, {
278278
});
279279
```
280280

281-
So, we can set many properties at once.
281+
أي أنّنا نقدر على ضبط أكثر من خاصية معًا.
282282

283283
## Object.getOwnPropertyDescriptors
284284

285-
To get all property descriptors at once, we can use the method [Object.getOwnPropertyDescriptors(obj)](mdn:js/Object/getOwnPropertyDescriptors).
285+
لجلب كلّ واصفات الخصائص معًا, يمكننا إستعمال الطريقة [Object.getOwnPropertyDescriptors(obj)](mdn:js/Object/getOwnPropertyDescriptors).
286286

287-
Together with `Object.defineProperties` it can be used as a "flags-aware" way of cloning an object:
287+
بدمجه مع `Object.defineProperties` يمكن إستخدامها لنسخ الكائنات "ونحن على علمٍ براياتها":
288288

289289
```js
290290
let clone = Object.defineProperties({}, Object.getOwnPropertyDescriptors(obj));
291291
```
292292

293-
Normally when we clone an object, we use an assignment to copy properties, like this:
293+
فعادةً حين ننسخ كائنًا, نستعمل الإسناد لنسخ الخصائص، هكذا:
294294

295295
```js
296296
for (let key in user) {
297297
clone[key] = user[key]
298298
}
299299
```
300300

301-
...But that does not copy flags. So if we want a "better" clone then `Object.defineProperties` is preferred.
301+
...و لكن هذا لا ينسخ الرايات. لذا إذا كنا نريد نسخ "أفضل" سيكون إستخدام `Object.defineProperties` أفضل.
302302

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` تُعيد *كل* واصِفات الخصائص بما فيها الرمزية.
304304

305-
## Sealing an object globally
305+
## إغلاق الكائنات على المستوى العام
306306

307-
Property descriptors work at the level of individual properties.
307+
تعمل واصِفات الخصائص على مستوى الخصائص منفردةً. هناك أيضًا توابِع تقصر الوصول إلى الكائن كلّه.
308308

309-
There are also methods that limit access to the *whole* object:
309+
يوجد ايضاً تحدد الدخول الى الكائن *كله* :
310310

311311
[Object.preventExtensions(obj)](mdn:js/Object/preventExtensions)
312-
: Forbids the addition of new properties to the object.
312+
: يمنع إضافة خصائص جديدة إلى الكائن.
313313

314314
[Object.seal(obj)](mdn:js/Object/seal)
315-
: Forbids adding/removing of properties. Sets `configurable: false` for all existing properties.
315+
: يمنع إضافة الخصائص وإزالتها. يقوم بوضع `configurable: false` لكل الخصائص الموجودة.
316316

317317
[Object.freeze(obj)](mdn:js/Object/freeze)
318-
: Forbids adding/removing/changing of properties. Sets `configurable: false, writable: false` for all existing properties.
318+
: يمنع إضافة الخصائص أو إزالتها أو تغييرها. يقوم بوضع `configurable: false, writable: false` لكل الخصائص الموجودة.
319319

320-
And also there are tests for them:
320+
كما أنّ هناك توابِع أخرى تفحص تلك المزايا:
321321

322322
[Object.isExtensible(obj)](mdn:js/Object/isExtensible)
323-
: Returns `false` if adding properties is forbidden, otherwise `true`.
323+
: يُعيد `false` لو كان ممنوعًا إضافة الخصائص, غير ذلك `true`.
324324

325325
[Object.isSealed(obj)](mdn:js/Object/isSealed)
326-
: Returns `true` if adding/removing properties is forbidden, and all existing properties have `configurable: false`.
326+
: يُعيد `true` لو كان ممنوعًا إضافة الخصائص أو إزالتها، وكانت كلّ خصائص الكائن الموجودة ممنوعة من قابلية إعادة الضبط `configurable: false`.
327327

328328
[Object.isFrozen(obj)](mdn:js/Object/isFrozen)
329-
: Returns `true` if adding/removing/changing properties is forbidden, and all current properties are `configurable: false, writable: false`.
329+
: يُعيد `true` إذا كان إضافة/حذف/تعديل الخصائص ممنوعاً, و كل الخصائص الحالية `configurable: false, writable: false`.
330330

331-
These methods are rarely used in practice.
331+
أمّا على أرض الواقع، فنادرًا ما نستعمل تلك الطرق.

0 commit comments

Comments
 (0)