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
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/01-object/article.md
+83-82Lines changed: 83 additions & 82 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,101 +1,101 @@
1
1
2
-
# Objects
2
+
# الكائنات
3
3
4
-
As we know from the chapter <info:types>, there are eight data types in JavaScript. Seven of them are called "primitive", because their values contain only a single thing (be it a string or a number or whatever).
4
+
كما عرفنا من فصل[أنواع البيانات](info:types), هنالك ثماني أنواع للبيانات في الجافا اسكريبت.سبعة منهم يسمون "أوليةprimitive",لأن قيمهم تحتوي شيئا واحداً (لتكن سلسلة نصية أو رقم أو أي شيْ)
5
5
6
-
In contrast, objects are used to store keyed collections of various data and more complex entities. In JavaScript, objects penetrate almost every aspect of the language. So we must understand them first before going in-depth anywhere else.
6
+
في المقابل,تستخدم الكائنات لحفظ مجموعات keyed collections من مختلف البيانات و الكيانات المركبة.في الجافا اسكريبت,تدخل الكائنات تقريبا في كل جانب من جوانب اللغة. لذا يتوجب علينا فهمها قبل التعمق في أي شيئ آخر.
7
7
8
-
An object can be created with figure brackets `{…}`with an optional list of *properties*. A property is a "key: value" pair, where `key` is a string (also called a "property name"), and `value` can be anything.
8
+
يمكن إنشاء أي كائن باستخدام الأقواس المعقوصة `{…}`مع قائمة اختيارية بالخاصيات. الخاصية هي زوج من "مفتاح: قيمة" (`key: value`) إذ يكون المفتاح عبارة عن نص (يُدعى "اسم الخاصية")، والقيمة يمكن أن تكون أي شيء.
9
9
10
-
We can imagine an object as a cabinet with signed files. Every piece of data is stored in its file by the key. It's easy to find a file by its name or add/remove a file.
10
+
يمكننا تخيل الكائن كخزانة تحوي ملفات. يُخزن كل جزء من هذه البيانات في الملف الخاص به باستخدام المفتاح. يمكن إيجاد، أو إضافة، أو حذف ملف باستخدام اسمه.
11
11
12
12

13
13
14
-
An empty object ("empty cabinet") can be created using one of two syntaxes:
14
+
يمكن إنشاء كائن فارغ ("خزانة فارغة") باستخدام إحدى تركيبتين:
15
15
16
16
```js
17
-
let user =newObject(); // "object constructor" syntax
18
-
let user = {}; // "object literal" syntax
17
+
let user =newObject(); // "object constructor" syntax صيغة منشئ الكائن
18
+
let user = {}; // "object literal" syntax
19
19
```
20
20
21
21

22
22
23
-
Usually, the figure brackets `{...}`are used. That declaration is called an *object literal*.
23
+
تُستخدم الأقواس المعقوصة `{...}`عادة، وهذا النوع من التصريح يُسمى «*الصياغة المختصرة لتعريف كائن*» (*object literal*).
24
24
25
-
## Literals and properties
25
+
## القيم المُجرَّدة والخاصيات
26
26
27
-
We can immediately put some properties into `{...}`as "key: value" pairs:
27
+
يمكننا إضافة بعض الخاصيات (properties) إلى الكائن المعرَّف بالأقواس `{...}`مباشرة بشكل أزواج "مفتاح: قيمة":
28
28
29
29
```js
30
-
let user = { // an object
31
-
name:"John", //by key "name" store value "John"
32
-
age:30//by key "age" store value 30
30
+
let user = { // an object كائن
31
+
name:"John", //خزن القيمة "John" عبر المفتاح "name"
32
+
age:30//خزن القيمة "30" عبر المفتاح "age"
33
33
};
34
34
```
35
35
36
-
A property has a key (also known as "name" or "identifier") before the colon `":"`and a value to the right of it.
36
+
لدى كل خاصية مفتاح (يُدعى أيضًا "اسم " أو "مُعَرِّف") قبل النقطتين `":"`وقيمة لهذه الخاصية بعد النقطتين.
37
37
38
-
In the `user` object, there are two properties:
38
+
يوجد خاصيتين في الكائن `user`:
39
39
40
-
1.The first property has the name `"name"`and the value `"John"`.
41
-
2.The second one has the name `"age"`and the value `30`.
40
+
1.اسم الخاصية الأولى هو `"name"`وقيمتها هي `"John"`.
41
+
2.اسم الخاصية الثانية هو `"age"`وقيمتها هي `"30"`.
42
42
43
-
The resulting `user`object can be imagined as a cabinet with two signed files labeled "name" and "age".
43
+
يمكن تخيل الكائن السابق `user`كخزانة بملفين مُسَمَّيان "name" و "age".
44
44
45
45

46
46
47
-
We can add, remove and read files from it any time.
47
+
يمكننا إضافة، وحذف، وقراءة الملفات من الخزانة في أي وقت.
48
48
49
-
Property values are accessible using the dot notation:
49
+
يمكن الوصول إلى قيم الخاصيات باستخدام الصيغة النُقَطية (dot notation):
50
50
51
51
```js
52
-
// get property values of the object:
52
+
//الحصول على قيم خصائص الكائن:
53
53
alert( user.name ); // John
54
54
alert( user.age ); // 30
55
55
```
56
56
57
-
The value can be of any type. Let's add a boolean one:
57
+
يمكن للقيمة أن تكون من أي نوع، لِنُضِف قيمة من نوع بيانات منطقية (boolean):
58
58
59
59
```js
60
60
user.isAdmin=true;
61
61
```
62
62
63
63

64
64
65
-
To remove a property, we can use `delete`operator:
65
+
يمكننا استخدام المُعامِل `delete`لحذف خاصية:
66
66
67
67
```js
68
68
deleteuser.age;
69
69
```
70
70
71
71

72
72
73
-
We can also use multiword property names, but then they must be quoted:
73
+
يمكننا أيضا استخدام خاصيات بأسماء تحوي أكثر من كلمة، لكن يجب وضعها بين علامات الاقتباس "":
74
74
75
75
```js
76
76
let user = {
77
77
name:"John",
78
78
age:30,
79
-
"likes birds":true//multiword property name must be quoted
79
+
"likes birds":true//يجب أن تكون الخاصية ذات الاسم المُحتوي على أكثر من كلمة بين علامتي اقتباس
80
80
};
81
81
```
82
82
83
83

84
84
85
85
86
-
The last property in the list may end with a comma:
86
+
يمكن إضافة فاصلة بعد آخر خاصية في القائمة:
87
87
```js
88
88
let user = {
89
89
name:"John",
90
90
age:30*!*,*/!*
91
91
}
92
92
```
93
-
That is called a "trailing" or "hanging" comma. Makes it easier to add/remove/move around properties, because all lines become alike.
93
+
وهذا يسمى فاصلة "زائدة" أو "معلقة". يجعل من السهل إضافة / إزالة / نقل الخصائص ، لأن جميع الأسطر تصبح متشابهة.
94
94
95
-
````smart header="Object with const can be changed"
96
-
Please note: an object declared as `const` *can* be modified.
95
+
````smart header="الكائن المعرف بأنه ثابت يمكنه أن يتغير"
96
+
يرجى ملاحظة: يمكن تعديل كائن معلن على أنه ثابت.
97
97
98
-
For instance:
98
+
مثلاً:
99
99
100
100
```js run
101
101
const user = {
@@ -109,55 +109,55 @@ user.name = "Pete"; // (*)
109
109
alert(user.name); // Pete
110
110
```
111
111
112
-
It might seem that the line `(*)` would cause an error, but no. The `const` fixes the value of `user`, but not its contents.
112
+
قد يبدو أن الخط `(*)` سيسبب خطأ ، لكن لا. يُحدِّد "const" قيمة "user" ، وليس إصلاح محتوياتها.
113
113
114
-
The `const` would give an error only if we try to set `user=...` as a whole.
114
+
لن يُظهر "const" خطأ إلا إذا حاولنا تعيين "user = ...` ككل.
115
115
116
-
There's another way to make constant object properties, we'll cover it later in the chapter <info:property-descriptors>.
116
+
هناك طريقة أخرى لعمل خصائص كائن ثابتة ، وسنتناولها لاحقًا في الفصل<info:property-descriptors>.
117
117
````
118
118
119
-
## Square brackets
119
+
## الأقواس المربعة
120
120
121
-
For multiword properties, the dot access doesn't work:
121
+
لا تعمل طريقة الوصول إلى الخاصيات ذات الأسماء المحتوية على أكثر من كلمة باستخدام الصيغة النُقَطية:
122
122
123
123
```js run
124
-
//this would give a syntax error
124
+
//سيعطي هذا خطأ في الصياغة
125
125
user.likes birds =true
126
126
```
127
127
128
-
JavaScript doesn't understand that. It thinks that we address `user.likes`, and then gives a syntax error when comes across unexpected `birds`.
128
+
لا تفهم الجافا اسكريبت هذه الصيغه. يعتقد أننا نتعامل مع "user.likes" ، ثم تعطي خطأ في بناء الجملة عندما يصادف "birds" غير متوقعة
129
129
130
-
The dot requires the key to be a valid variable identifier. That implies: contains no spaces, doesn't start with a digit and doesn't include special characters (`$`and `_` are allowed).
130
+
تتطلب النقطة أن يكون المفتاح معرفًا متغيرًا صالحًا. هذا يعني: أنه لا يحتوي على مسافات ، ولا يبدأ برقم ولا يتضمن أحرفًا خاصة (يُسمح بـ `$`و` _`).
131
131
132
-
There's an alternative "square bracket notation" that works with any string:
132
+
هناك بديل "رمز القوس المربع" يعمل مع أي سلسلة:
133
133
134
134
```js run
135
135
let user = {};
136
136
137
-
//set
137
+
//تعيين قيمة
138
138
user["likes birds"] =true;
139
139
140
-
//get
140
+
//الحصول على قيمة
141
141
alert(user["likes birds"]); // true
142
142
143
-
//delete
143
+
//حذف
144
144
delete user["likes birds"];
145
145
```
146
146
147
-
Now everything is fine. Please note that the string inside the brackets is properly quoted (any type of quotes will do).
147
+
الآن كل شيء على ما يرام. يرجى ملاحظة أن السلسلة داخل الأقواس مقتبسة بشكل صحيح (أي نوع من علامات الاقتباس ستفعل).
148
148
149
-
Square brackets also provide a way to obtain the property name as the result of any expression -- as opposed to a literal string -- like from a variable as follows:
149
+
توفر الأقواس المربعة أيضًا جلب اسم خاصية ناتجة عن قيمة أي تعبير - على عكس السلسلة الحرفية - مثل المتغير كما يلي:
150
150
151
151
```js
152
152
let key ="likes birds";
153
153
154
-
//same as user["likes birds"] = true;
154
+
//يشبه تماماً user["likes birds"] = true;
155
155
user[key] =true;
156
156
```
157
157
158
-
Here, the variable `key`may be calculated at run-time or depend on the user input. And then we use it to access the property. That gives us a great deal of flexibility.
158
+
هنا, المتغير`key`يمكن حسابه وقت التنفيذ run-time أو اعتمادا على ما يدخله المستخدم. من ثم نستخدمها للوصول إلى الخاصية.وهذا يمنحنا قدراً كبيراً من المرونة.
159
159
160
-
For instance:
160
+
مثلاً:
161
161
162
162
```js run
163
163
let user = {
@@ -167,11 +167,11 @@ let user = {
167
167
168
168
let key =prompt("What do you want to know about the user?", "name");
169
169
170
-
//access by variable
170
+
//الوصول عن طريق المتغير
171
171
alert( user[key] ); // John (if enter "name")
172
172
```
173
173
174
-
The dot notation cannot be used in a similar way:
174
+
لا يمكن استخدام رمز النقطة بطريقة مماثلة لما مضى:
175
175
176
176
```js run
177
177
let user = {
@@ -183,40 +183,40 @@ let key = "name";
183
183
alert( user.key ) // undefined
184
184
```
185
185
186
-
### Computed properties
186
+
### خصائص محسوبة (computed properties)
187
187
188
-
We can use square brackets in an object literal, when creating an object. That's called *computed properties*.
188
+
يمكننا استخدام الأقواس المربعة في كائن حرفي object literal، عند إنشاء كائن. وهذا ما يسمى * الخصائص المحسوبة computed properties*.
189
189
190
-
For instance:
190
+
مثلا:
191
191
192
192
```js run
193
193
let fruit =prompt("Which fruit to buy?", "apple");
194
194
195
195
let bag = {
196
196
*!*
197
-
[fruit]:5, //the name of the property is taken from the variable fruit
197
+
[fruit]:5, //يؤخذ اسم الخاصية من المتغير fruit
198
198
*/!*
199
199
};
200
200
201
201
alert( bag.apple ); // 5 if fruit="apple"
202
202
```
203
203
204
-
The meaning of a computed property is simple: `[fruit]`means that the property name should be taken from`fruit`.
204
+
معنى computed property بسيط: `[fruit]`تعني أن اسم الخاصية يجب أن يؤخذ من`fruit`.
205
205
206
-
So, if a visitor enters `"apple"`, `bag`will become`{apple: 5}`.
206
+
لذا, إذا أدخل الزائر `"apple"`, `bag`ستتحول`{apple: 5}`.
207
207
208
-
Essentially, that works the same as:
208
+
يعمل الأمر السابق بالطريقة التالية ذاتها:
209
209
```js run
210
210
let fruit =prompt("Which fruit to buy?", "apple");
211
211
let bag = {};
212
212
213
-
//take property name from the fruit variable
213
+
//خذ اسم الخاصية من متغير fruit
214
214
bag[fruit] =5;
215
215
```
216
216
217
-
...But looks nicer.
217
+
...يبدو ذلك أفضل.
218
218
219
-
We can use more complex expressions inside square brackets:
219
+
يمكننا استخدام تعبيرات أكثر تعقيداً داخل الأقواس المربعة:
220
220
221
221
```js
222
222
let fruit ='apple';
@@ -225,15 +225,15 @@ let bag = {
225
225
};
226
226
```
227
227
228
-
Square brackets are much more powerful than the dot notation. They allow any property names and variables. But they are also more cumbersome to write.
228
+
الأقواس المربعة أقوى بكثير من استخدام الصيغة النُقطية.حيث تسمح باستخدام أي أسماء خصائص و متغيرات. لكنها أيضا أكثر إرهاقاً في الكتابة.
229
229
230
-
So most of the time, when property names are known and simple, the dot is used. And if we need something more complex, then we switch to square brackets.
230
+
لذلك معظم الوقت, حينما يكون اسم خاصية معروفا أو بسيطا, تستخدم الصيغة النُقطية . وإذا أردنا شيئاً أكثر تعقيدا, ننتقل إلى استخدام الأقواس المربعة.
231
231
232
-
## Property value shorthand
232
+
## اختصار قيمة الخاصية(Property value shorthand)
233
233
234
-
In real code we often use existing variables as values for property names.
234
+
في الكود الحقيقي ، غالبًا ما نستخدم المتغيرات الموجودة كقيم لأسماء الخصائص.
235
235
236
-
For instance:
236
+
مثلاً:
237
237
238
238
```js run
239
239
functionmakeUser(name, age) {
@@ -248,23 +248,23 @@ let user = makeUser("John", 30);
248
248
alert(user.name); // John
249
249
```
250
250
251
-
In the example above, properties have the same names as variables. The use-case of making a property from a variable is so common, that there's a special *property value shorthand* to make it shorter.
251
+
في المثال السابق, تمتلك الخصائص نفس أسماء المتغيرات. حالة عمل خاصية من متغير هي أمر شائع جدا, that ذلك أنه من المميز وجود *اختصار قيمة الخاصية* لجعلها مختصرة .
252
252
253
-
Instead of`name:name`we can just write `name`, like this:
253
+
بدلاً من`name:name`يمكننا فقط كتابة `name`, كهذا المثال:
254
254
255
255
```js
256
256
functionmakeUser(name, age) {
257
257
*!*
258
258
return {
259
-
name, //same as name: name
260
-
age, //same as age: age
259
+
name, //تماماً مثل name: name
260
+
age, //تماماً مثل age: age
261
261
// ...
262
262
};
263
263
*/!*
264
264
}
265
265
```
266
266
267
-
We can use both normal properties and shorthands in the same object:
267
+
يمكننا استخدام كل من الخصائص العادية و والاختصارات كليهما في نفس الكائن object:
268
268
269
269
```js
270
270
let user = {
@@ -274,14 +274,14 @@ let user = {
274
274
```
275
275
276
276
277
-
## Property names limitations
277
+
## قيود أسماء الخصائص Property names limitations
278
278
279
-
As we already know, a variable cannot have a name equal to one of language-reserved words like "for", "let", "return" etc.
279
+
كما نعلم, لا يمكن للمتغير أن يمتلك اسماً يساوي واحداً من الكلمات المحفوظة للغة language-reserved words مثل "for", "let", "return" إلخ.
280
280
281
-
But for an object property, there's no such restriction:
281
+
لكن بالنسبة لخاصية في كائن, لا توجد مثل هذه القيود:
282
282
283
283
```js run
284
-
//these properties are all right
284
+
//كل هذه الخصائص صحيحة
285
285
let obj = {
286
286
for:1,
287
287
let:2,
@@ -291,35 +291,36 @@ let obj = {
291
291
alert( obj.for+obj.let+obj.return ); // 6
292
292
```
293
293
294
-
In short, there are no limitations on property names. They can be any strings or symbols (a special type for identifiers, to be covered later).
295
294
296
-
Other types are automatically converted to strings.
295
+
باختصار, ليس هناك أي قيود لأسماء الخصائص property names. يمكن أن يكونوا أي حرف أو رمز (نوع مميز من identifiers, سيتم الحديث عنه لاحقاً).
296
+
297
+
الأنواع الأخرى تتحول تلقائياً لسلاسل نصية strings.
297
298
298
-
For instance, a number `0`becomes a string`"0"`when used as a property key:
299
+
مثلاً, رقم `0`يتحول إلى حرف`"0"`عند استخدامه كاسم خاصية property key:
299
300
300
301
```js run
301
302
let obj = {
302
303
0:"test"// same as "0": "test"
303
304
};
304
305
305
-
//both alerts access the same property (the number 0 is converted to string "0")
306
+
//كليهما يسمحان بالوصول إلى الخاصية (رقم 0 يتحول إلى حرف "0")
306
307
alert( obj["0"] ); // test
307
308
alert( obj[0] ); // test (same property)
308
309
```
309
310
310
-
There's a minor gotcha with a special property named `__proto__`. We can't set it to a non-object value:
311
+
There's a minor gotcha with a special property named `__proto__`. لا يمكننا استخدام الاسم على أنَّه قيمة لغير كائن:
311
312
312
313
```js run
313
314
let obj = {};
314
315
obj.__proto__=5; // assign a number
315
316
alert(obj.__proto__); // [object Object] - the value is an object, didn't work as intended
316
317
```
317
318
318
-
As we see from the code, the assignment to a primitive `5`is ignored.
319
+
كما نرى من الكود , إعطاء قيمة أولية primitive `5`يتم تجاهلها.
319
320
320
-
We'll cover the special nature of `__proto__`in[subsequent chapters](info:prototype-inheritance), and suggest the [ways to fix](info:prototype-methods)such behavior.
321
+
سوف نغطي طبيعة `__proto__`في[subsequent chapters](info:prototype-inheritance), واقتراح [ways to fix](info:prototype-methods)مثل هذا السلوك.
321
322
322
-
## Property existence test, "in" operator
323
+
## فحص الكينونة, "in" معامل
323
324
324
325
A notable feature of objects in JavaScript, compared to many other languages, is that it's possible to access any property. There will be no error if the property doesn't exist!
0 commit comments