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/02-object-copy/article.md
+36-36Lines changed: 36 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -1,65 +1,65 @@
1
1
# Object references and copying
2
2
3
-
One of the fundamental differences of objects versus primitives is that objects are stored and copied "by reference", whereas primitive values: strings, numbers, booleans, etc -- are always copied "as a whole value".
3
+
أحد الاختلافات الأساسية بين الكائنات والقيم الأساسية هو أن الكائنات تخزن وتنسخ "ضمنيًا" وبالمقابل، يتم نسخ القيم الأساسية بالكامل.
4
4
5
-
That's easy to understand if we look a bit under the hood of what happens when we copy a value.
5
+
هذا سهل الفهم إذا نظرنا قليلاً تحت الغطاء لما يحدث عند نسخ القيمة.
6
6
7
-
Let's start with a primitive, such as a string.
7
+
لنبدأ بالقيمة الأساسية، مثل السلسلة.
8
8
9
-
Here we put a copy of `message`into`phrase`:
9
+
هنا نضع نسخة من `message`في`phrase`:
10
10
11
11
```js
12
12
let message ="Hello!";
13
13
let phrase = message;
14
14
```
15
15
16
-
As a result we have two independent variables, each one storing the string`"Hello!"`.
16
+
نحن الآن لدينا متغيران مستقلان، يخزن كل منهما السلسانة`"Hello!"`.
17
17
18
18

19
19
20
-
Quite an obvious result, right?
20
+
نتيجة واضحة جدًا، أليس كذلك؟
21
21
22
-
Objects are not like that.
22
+
الكائنات ليست كذلك.
23
23
24
-
**A variable assigned to an object stores not the object itself, but its "address in memory" -- in other words "a reference" to it.**
24
+
**يخزن المتغير الذي يشير إلى كائن ليس الكائن نفسه، ولكن "عنوانه في الذاكرة" - بعبارة أخرى "مرجعًا" له.**
25
25
26
-
Let's look at an example of such a variable:
26
+
لنلقي نظرة على مثال لمتغير من هذا النوع:
27
27
28
28
```js
29
29
let user = {
30
30
name:"John",
31
31
};
32
32
```
33
33
34
-
And here's how it's actually stored in memory:
34
+
وهنا كيف يتم تخزينه في الذاكرة:
35
35
36
36

37
37
38
-
The object is stored somewhere in memory (at the right of the picture), while the`user`variable (at the left) has a "reference" to it.
38
+
يتم تخزين الكائن في مكان ما في الذاكرة (على اليمين من الصورة)، في حين يحتوي المتغير`user`(على اليسار) على "مرجع" إلى هذا المكان.
39
39
40
-
We may think of an object variable, such as `user`, as like a sheet of paper with the address of the object on it.
40
+
يمكننا التفكير في متغير الكائن، مثل `user`، كورقة ورق بها عنوان الكائن.
41
41
42
-
When we perform actions with the object, e.g. take a property`user.name`, the JavaScript engine looks at what's at that address and performs the operation on the actual object.
42
+
عندما نقوم بإجراء أي عمليات على الكائن، مثل الوصول إلى خاصية`user.name`، يقوم محرك JavaScript بالنظر إلى ما يوجد في ذلك العنوان وينفذ العملية على الكائن الفعلي.
43
43
44
-
Now here's why it's important.
44
+
والآن هنا هو السبب في أهمية هذا الأمر.
45
45
46
-
**When an object variable is copied, the reference is copied, but the object itself is not duplicated.**
46
+
**عند نسخ متغير الكائن ، يتم نسخ المرجع ، ولكن الكائن نفسه لا يتم استنساخه.**
47
47
48
-
For instance:
48
+
على سبيل المثال:
49
49
50
50
```js no-beautify
51
51
let user = { name:"John" };
52
52
53
-
let admin = user; //ينسخ المؤشر
53
+
let admin = user; //المرجع يتم نسخه
54
54
```
55
55
56
-
Now we have two variables, each storing a reference to the same object:
56
+
الآن لدينا متغيران، يخزن كل منهما مرجعًا إلى نفس الكائن نفسه:
57
57
58
58

59
59
60
-
As you can see, there's still one object, but now with two variables that reference it.
60
+
كما يمكن رؤية، لا يوجد سوى كائن واحد، ولكن الآن بوجود متغيرين يشيران إليه.
61
61
62
-
We can use either variable to access the object and modify its contents:
62
+
يمكننا استخدام أي من المتغيرين للوصول إلى الكائن وتعديل محتوياته:
63
63
64
64
```js run
65
65
let user = { name:'John' };
@@ -73,7 +73,7 @@ admin.name = 'Pete'; // تم تغييرها بواسطة المؤشر "admin"
73
73
alert(*!*user.name*/!*); //'Pete', التغيرات مرئية بواسطة مؤشر "user"
74
74
```
75
75
76
-
It's as if we had a cabinet with two keys and used one of them (`admin`) to get into it and make changes. Then, if we later use another key (`user`), we are still opening the same cabinet and can access the changed contents.
76
+
هذا يعني أنه وكأننا لدينا خزانة بها مفاتيحان واستخدمنا واحدة منهما (`admin`) للوصول إلى المحتويات وإجراء التغييرات، ثم إذا استخدمنا المفتى الأخرى (`user`) في وقت لاحق، فنحن لا نفتح سوى نفس الخزانة ويمكننا الوصول إلى المحتويات المغيرة.
And here two independent objects are not equal, even though they look alike (both are empty):
92
+
وهنا لا يتساوى كائنان مستقلان، على الرغم من أنهما يبدوان متشابهين (كلاهما فارغ):
93
93
94
94
```js run
95
95
let a = {};
@@ -98,15 +98,15 @@ let b = {}; // كائنان منفصلان
98
98
alert(a == b); // false
99
99
```
100
100
101
-
For comparisons like `obj1 > obj2`or for a comparison against a primitive `obj ==5`, objects are converted to primitives. We'll study how object conversions work very soon, but to tell the truth, such comparisons are needed very rarely -- usually they appear as a result of a programming mistake.
101
+
للمقارنات مثل `obj1 > obj2`أو للمقارنة مع قيمة أولية `obj ==5`، يتم تحويل الكائنات إلى قيم أولية. سندرس كيفية عمل تحويلات الكائنات قريبًا جدًا، ولكن لنقل الحقيقة، تحتاج هذه المقارنات نادرًا جدًا - عادة ما تظهر نتيجة خطأ في البرمجة.
102
102
103
-
## Cloning and merging, Object.assign [#cloning-and-merging-object-assign]
That's also doable, but a little bit more difficult, because there's no built-in method for that in JavaScript. But there is rarely a need -- copying by reference is good most of the time.
109
+
هذا أيضًا قابل للتنفيذ، ولكنه أكثر صعوبة قليلاً، لأنه لا يوجد طريقة مدمجة لذلك في جافا سكريبت. ولكن نادرًا ما يكون هناك حاجة - فالنسخ بالإشارة جيد في معظم الأحيان.
110
110
111
111
لكن إذا أردنا ذلك حقًا يمكننا فعل ذلك عن طريق عمل كائن آخر والمرور على خواص الكائن الحالي ونسخها واحدة تلو الأخرى.
112
112
@@ -189,9 +189,9 @@ let clone = Object.assign({}, user);
189
189
190
190
## النسخ المتداخل
191
191
192
-
There are also other methods of cloning an object, e.g. using the [spread syntax](info:rest-parameters-spread) `clone = {...user}`, covered later in the tutorial.
192
+
هناك أيضًا طرق أخرى لاستنساخ كائن، على سبيل المثال باستخدام [spread syntax](info:rest-parameters-spread) `clone = {...user}`، والتي سيتم تغطيتها في وقت لاحق في البرنامج التعليمي.
193
193
194
-
## Nested cloning
194
+
## النسخ الشامل
195
195
196
196
مثل هذا:
197
197
@@ -229,14 +229,14 @@ user.sizes.width++; // تغيير الخاصية من مكان
229
229
alert(clone.sizes.width); // 51, يجعل التغيير مئي في المكان الآخر
230
230
```
231
231
232
-
To fix that, we should use a cloning loop that examines each value of `user[key]`and, if it's an object, then replicate its structure as well. That is called a "deep cloning".
232
+
لحل هذه المشكلة، يجب علينا استخدام حلقة نسخ تفحص كل قيمة في `user[key]`وإذا كانت كائنًا، فإنه يتم تكرار هيكله أيضًا. يُسمى ذلك "نسخًا عميقًا".
233
233
234
-
We can use recursion to implement it. Or, to not reinvent the wheel, take an existing implementation, for instance [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep) from the JavaScript library [lodash](https://lodash.com).
234
+
يمكن استخدام التكرار لتنفيذ نسخة عميقة، أو يمكن استخدام تنفيذ موجود لعدم اختراع العجلة، على سبيل المثال [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep) من مكتبة JavaScript [lodash](https://lodash.com).
235
235
236
-
````smart header="Const objects can be modified"
237
-
An important side effect of storing objects as references is that an object declared as `const` *can* bemodified.
236
+
````smart header="يمكن تعديل الكائنات التي تم تعريفها بـ const"
237
+
آثار جانبية مهمة لتخزين الكائنات كمراجع هو أنه يمكن تعديل كائن معرف بـ `const`.
238
238
239
-
For instance:
239
+
على سبيل المثال:
240
240
241
241
```js run
242
242
const user = {
@@ -250,16 +250,16 @@ user.name = "Pete"; // (*)
250
250
alert(user.name); // Pete
251
251
```
252
252
253
-
It might seem that the line `(*)`would cause an error, but it does not. The value of`user`is constant, it must always reference the same object, but properties of that object are free to change.
253
+
قد يبدو أن السطر `(*)`سيتسبب في حدوث خطأ، ولكن لا يحدث خطأ. قيمة `user`ثابتة، يجب أن يشير دائمًا إلى نفس الكائن، ولكن يمكن تغيير خصائص هذا الكائن.
254
254
255
-
In other words, the `const user`gives an error only if we try to set `user=...`as a whole.
255
+
بمعنى آخر، يتم إصدار خطأ عند استخدام `const user`فقط إذا حاولنا تعيين `user=...`ككل.
256
256
257
-
That said, if we really need to make constant object properties, it's also possible, but using totally different methods. We'll mention that in the chapter<info:property-descriptors>.
257
+
ومع ذلك، إذا كنا بحاجة فعلًا إلى جعل خصائص الكائن ثابتة، فمن الممكن ذلك باستخدام طرق مختلفة تمامًا. سنذكر ذلك في الفصل<info:property-descriptors>.
258
258
````
259
259
260
260
## ملخص
261
261
262
-
Objects are assigned and copied by reference. In other words, a variable stores not the "object value", but a "reference" (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object itself.
262
+
الكائنات تتم تعيينها ونسخها بالمرجع. وبمعنى آخر، يخزن المتغير ليس "قيمة الكائن" ولكن "مرجع" (عنوان في الذاكرة) للقيمة. لذلك، عند نسخ هذا المتغير أو تمريره كوسيط لدالة، يتم نسخ هذا المرجع، وليس الكائن نفسه.
263
263
264
264
كل العمليات التي تتم بواسطة النسخة (مثل إضافة وحذف الخواص) تحدث على نفس الكائن.
0 commit comments