Skip to content

Commit 81c8321

Browse files
committed
Objects
1 parent b33eb77 commit 81c8321

1 file changed

Lines changed: 83 additions & 82 deletions

File tree

1-js/04-object-basics/01-object/article.md

Lines changed: 83 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,101 @@
11

2-
# Objects
2+
# الكائنات
33

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",لأن قيمهم تحتوي شيئا واحداً (لتكن سلسلة نصية أو رقم أو أي شيْ)
55

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 من مختلف البيانات و الكيانات المركبة.في الجافا اسكريبت,تدخل الكائنات تقريبا في كل جانب من جوانب اللغة. لذا يتوجب علينا فهمها قبل التعمق في أي شيئ آخر.
77

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`) إذ يكون المفتاح عبارة عن نص (يُدعى "اسم الخاصية")، والقيمة يمكن أن تكون أي شيء.
99

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+
يمكننا تخيل الكائن كخزانة تحوي ملفات. يُخزن كل جزء من هذه البيانات في الملف الخاص به باستخدام المفتاح. يمكن إيجاد، أو إضافة، أو حذف ملف باستخدام اسمه.
1111

1212
![](object.svg)
1313

14-
An empty object ("empty cabinet") can be created using one of two syntaxes:
14+
يمكن إنشاء كائن فارغ ("خزانة فارغة") باستخدام إحدى تركيبتين:
1515

1616
```js
17-
let user = new Object(); // "object constructor" syntax
18-
let user = {}; // "object literal" syntax
17+
let user = new Object(); // "object constructor" syntax صيغة منشئ الكائن
18+
let user = {}; // "object literal" syntax
1919
```
2020

2121
![](object-user-empty.svg)
2222

23-
Usually, the figure brackets `{...}` are used. That declaration is called an *object literal*.
23+
تُستخدم الأقواس المعقوصة `{...}` عادة، وهذا النوع من التصريح يُسمى «*الصياغة المختصرة لتعريف كائن*» (*object literal*).
2424

25-
## Literals and properties
25+
## القيم المُجرَّدة والخاصيات
2626

27-
We can immediately put some properties into `{...}` as "key: value" pairs:
27+
يمكننا إضافة بعض الخاصيات (properties) إلى الكائن المعرَّف بالأقواس `{...}` مباشرة بشكل أزواج "مفتاح: قيمة":
2828

2929
```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"
3333
};
3434
```
3535

36-
A property has a key (also known as "name" or "identifier") before the colon `":"` and a value to the right of it.
36+
لدى كل خاصية مفتاح (يُدعى أيضًا "اسم " أو "مُعَرِّف") قبل النقطتين `":"` وقيمة لهذه الخاصية بعد النقطتين.
3737

38-
In the `user` object, there are two properties:
38+
يوجد خاصيتين في الكائن `user`:
3939

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"`.
4242

43-
The resulting `user` object can be imagined as a cabinet with two signed files labeled "name" and "age".
43+
يمكن تخيل الكائن السابق `user` كخزانة بملفين مُسَمَّيان "name" و "age".
4444

4545
![user object](object-user.svg)
4646

47-
We can add, remove and read files from it any time.
47+
يمكننا إضافة، وحذف، وقراءة الملفات من الخزانة في أي وقت.
4848

49-
Property values are accessible using the dot notation:
49+
يمكن الوصول إلى قيم الخاصيات باستخدام الصيغة النُقَطية (dot notation):
5050

5151
```js
52-
// get property values of the object:
52+
//الحصول على قيم خصائص الكائن:
5353
alert( user.name ); // John
5454
alert( user.age ); // 30
5555
```
5656

57-
The value can be of any type. Let's add a boolean one:
57+
يمكن للقيمة أن تكون من أي نوع، لِنُضِف قيمة من نوع بيانات منطقية (boolean):
5858

5959
```js
6060
user.isAdmin = true;
6161
```
6262

6363
![user object 2](object-user-isadmin.svg)
6464

65-
To remove a property, we can use `delete` operator:
65+
يمكننا استخدام المُعامِل `delete` لحذف خاصية:
6666

6767
```js
6868
delete user.age;
6969
```
7070

7171
![user object 3](object-user-delete.svg)
7272

73-
We can also use multiword property names, but then they must be quoted:
73+
يمكننا أيضا استخدام خاصيات بأسماء تحوي أكثر من كلمة، لكن يجب وضعها بين علامات الاقتباس "":
7474

7575
```js
7676
let user = {
7777
name: "John",
7878
age: 30,
79-
"likes birds": true // multiword property name must be quoted
79+
"likes birds": true // يجب أن تكون الخاصية ذات الاسم المُحتوي على أكثر من كلمة بين علامتي اقتباس
8080
};
8181
```
8282

8383
![](object-user-props.svg)
8484

8585

86-
The last property in the list may end with a comma:
86+
يمكن إضافة فاصلة بعد آخر خاصية في القائمة:
8787
```js
8888
let user = {
8989
name: "John",
9090
age: 30*!*,*/!*
9191
}
9292
```
93-
That is called a "trailing" or "hanging" comma. Makes it easier to add/remove/move around properties, because all lines become alike.
93+
وهذا يسمى فاصلة "زائدة" أو "معلقة". يجعل من السهل إضافة / إزالة / نقل الخصائص ، لأن جميع الأسطر تصبح متشابهة.
9494

95-
````smart header="Object with const can be changed"
96-
Please note: an object declared as `const` *can* be modified.
95+
````smart header="الكائن المعرف بأنه ثابت يمكنه أن يتغير"
96+
يرجى ملاحظة: يمكن تعديل كائن معلن على أنه ثابت.
9797
98-
For instance:
98+
مثلاً:
9999
100100
```js run
101101
const user = {
@@ -109,55 +109,55 @@ user.name = "Pete"; // (*)
109109
alert(user.name); // Pete
110110
```
111111
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" ، وليس إصلاح محتوياتها.
113113
114-
The `const` would give an error only if we try to set `user=...` as a whole.
114+
لن يُظهر "const" خطأ إلا إذا حاولنا تعيين "user = ...` ككل.
115115
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>.
117117
````
118118

119-
## Square brackets
119+
## الأقواس المربعة
120120

121-
For multiword properties, the dot access doesn't work:
121+
لا تعمل طريقة الوصول إلى الخاصيات ذات الأسماء المحتوية على أكثر من كلمة باستخدام الصيغة النُقَطية:
122122

123123
```js run
124-
// this would give a syntax error
124+
// سيعطي هذا خطأ في الصياغة
125125
user.likes birds = true
126126
```
127127

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" غير متوقعة
129129

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+
تتطلب النقطة أن يكون المفتاح معرفًا متغيرًا صالحًا. هذا يعني: أنه لا يحتوي على مسافات ، ولا يبدأ برقم ولا يتضمن أحرفًا خاصة (يُسمح بـ `$` و` _`).
131131

132-
There's an alternative "square bracket notation" that works with any string:
132+
هناك بديل "رمز القوس المربع" يعمل مع أي سلسلة:
133133

134134
```js run
135135
let user = {};
136136

137-
// set
137+
// تعيين قيمة
138138
user["likes birds"] = true;
139139

140-
// get
140+
// الحصول على قيمة
141141
alert(user["likes birds"]); // true
142142

143-
// delete
143+
// حذف
144144
delete user["likes birds"];
145145
```
146146

147-
Now everything is fine. Please note that the string inside the brackets is properly quoted (any type of quotes will do).
147+
الآن كل شيء على ما يرام. يرجى ملاحظة أن السلسلة داخل الأقواس مقتبسة بشكل صحيح (أي نوع من علامات الاقتباس ستفعل).
148148

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+
توفر الأقواس المربعة أيضًا جلب اسم خاصية ناتجة عن قيمة أي تعبير - على عكس السلسلة الحرفية - مثل المتغير كما يلي:
150150

151151
```js
152152
let key = "likes birds";
153153

154-
// same as user["likes birds"] = true;
154+
// يشبه تماماً user["likes birds"] = true;
155155
user[key] = true;
156156
```
157157

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 أو اعتمادا على ما يدخله المستخدم. من ثم نستخدمها للوصول إلى الخاصية.وهذا يمنحنا قدراً كبيراً من المرونة.
159159

160-
For instance:
160+
مثلاً:
161161

162162
```js run
163163
let user = {
@@ -167,11 +167,11 @@ let user = {
167167

168168
let key = prompt("What do you want to know about the user?", "name");
169169

170-
// access by variable
170+
// الوصول عن طريق المتغير
171171
alert( user[key] ); // John (if enter "name")
172172
```
173173

174-
The dot notation cannot be used in a similar way:
174+
لا يمكن استخدام رمز النقطة بطريقة مماثلة لما مضى:
175175

176176
```js run
177177
let user = {
@@ -183,40 +183,40 @@ let key = "name";
183183
alert( user.key ) // undefined
184184
```
185185

186-
### Computed properties
186+
### خصائص محسوبة (computed properties)
187187

188-
We can use square brackets in an object literal, when creating an object. That's called *computed properties*.
188+
يمكننا استخدام الأقواس المربعة في كائن حرفي object literal، عند إنشاء كائن. وهذا ما يسمى * الخصائص المحسوبة computed properties*.
189189

190-
For instance:
190+
مثلا:
191191

192192
```js run
193193
let fruit = prompt("Which fruit to buy?", "apple");
194194

195195
let bag = {
196196
*!*
197-
[fruit]: 5, // the name of the property is taken from the variable fruit
197+
[fruit]: 5, // يؤخذ اسم الخاصية من المتغير fruit
198198
*/!*
199199
};
200200

201201
alert( bag.apple ); // 5 if fruit="apple"
202202
```
203203

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

206-
So, if a visitor enters `"apple"`, `bag` will become `{apple: 5}`.
206+
لذا, إذا أدخل الزائر `"apple"`, `bag` ستتحول `{apple: 5}`.
207207

208-
Essentially, that works the same as:
208+
يعمل الأمر السابق بالطريقة التالية ذاتها:
209209
```js run
210210
let fruit = prompt("Which fruit to buy?", "apple");
211211
let bag = {};
212212

213-
// take property name from the fruit variable
213+
// خذ اسم الخاصية من متغير fruit
214214
bag[fruit] = 5;
215215
```
216216

217-
...But looks nicer.
217+
...يبدو ذلك أفضل.
218218

219-
We can use more complex expressions inside square brackets:
219+
يمكننا استخدام تعبيرات أكثر تعقيداً داخل الأقواس المربعة:
220220

221221
```js
222222
let fruit = 'apple';
@@ -225,15 +225,15 @@ let bag = {
225225
};
226226
```
227227

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+
الأقواس المربعة أقوى بكثير من استخدام الصيغة النُقطية.حيث تسمح باستخدام أي أسماء خصائص و متغيرات. لكنها أيضا أكثر إرهاقاً في الكتابة.
229229

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+
لذلك معظم الوقت, حينما يكون اسم خاصية معروفا أو بسيطا, تستخدم الصيغة النُقطية . وإذا أردنا شيئاً أكثر تعقيدا, ننتقل إلى استخدام الأقواس المربعة.
231231

232-
## Property value shorthand
232+
## اختصار قيمة الخاصية(Property value shorthand)
233233

234-
In real code we often use existing variables as values for property names.
234+
في الكود الحقيقي ، غالبًا ما نستخدم المتغيرات الموجودة كقيم لأسماء الخصائص.
235235

236-
For instance:
236+
مثلاً:
237237

238238
```js run
239239
function makeUser(name, age) {
@@ -248,23 +248,23 @@ let user = makeUser("John", 30);
248248
alert(user.name); // John
249249
```
250250

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 ذلك أنه من المميز وجود *اختصار قيمة الخاصية* لجعلها مختصرة .
252252

253-
Instead of `name:name` we can just write `name`, like this:
253+
بدلاً من `name:name` يمكننا فقط كتابة `name`, كهذا المثال:
254254

255255
```js
256256
function makeUser(name, age) {
257257
*!*
258258
return {
259-
name, // same as name: name
260-
age, // same as age: age
259+
name, // تماماً مثل name: name
260+
age, // تماماً مثل age: age
261261
// ...
262262
};
263263
*/!*
264264
}
265265
```
266266

267-
We can use both normal properties and shorthands in the same object:
267+
يمكننا استخدام كل من الخصائص العادية و والاختصارات كليهما في نفس الكائن object :
268268

269269
```js
270270
let user = {
@@ -274,14 +274,14 @@ let user = {
274274
```
275275

276276

277-
## Property names limitations
277+
## قيود أسماء الخصائص Property names limitations
278278

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" إلخ.
280280

281-
But for an object property, there's no such restriction:
281+
لكن بالنسبة لخاصية في كائن, لا توجد مثل هذه القيود:
282282

283283
```js run
284-
// these properties are all right
284+
// كل هذه الخصائص صحيحة
285285
let obj = {
286286
for: 1,
287287
let: 2,
@@ -291,35 +291,36 @@ let obj = {
291291
alert( obj.for + obj.let + obj.return ); // 6
292292
```
293293

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).
295294

296-
Other types are automatically converted to strings.
295+
باختصار, ليس هناك أي قيود لأسماء الخصائص property names. يمكن أن يكونوا أي حرف أو رمز (نوع مميز من identifiers, سيتم الحديث عنه لاحقاً).
296+
297+
الأنواع الأخرى تتحول تلقائياً لسلاسل نصية strings.
297298

298-
For instance, a number `0` becomes a string `"0"` when used as a property key:
299+
مثلاً, رقم `0` يتحول إلى حرف `"0"` عند استخدامه كاسم خاصية property key:
299300

300301
```js run
301302
let obj = {
302303
0: "test" // same as "0": "test"
303304
};
304305

305-
// both alerts access the same property (the number 0 is converted to string "0")
306+
// كليهما يسمحان بالوصول إلى الخاصية (رقم 0 يتحول إلى حرف "0")
306307
alert( obj["0"] ); // test
307308
alert( obj[0] ); // test (same property)
308309
```
309310

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__`. لا يمكننا استخدام الاسم على أنَّه قيمة لغير كائن:
311312

312313
```js run
313314
let obj = {};
314315
obj.__proto__ = 5; // assign a number
315316
alert(obj.__proto__); // [object Object] - the value is an object, didn't work as intended
316317
```
317318

318-
As we see from the code, the assignment to a primitive `5` is ignored.
319+
كما نرى من الكود , إعطاء قيمة أولية primitive `5` يتم تجاهلها.
319320

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) مثل هذا السلوك.
321322

322-
## Property existence test, "in" operator
323+
## فحص الكينونة, "in" معامل
323324

324325
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!
325326

0 commit comments

Comments
 (0)