Skip to content

Commit d9a9d00

Browse files
authored
Merge pull request #286 from afilahkle/master
Object references and copying
2 parents ffdfc43 + 6a0335f commit d9a9d00

File tree

1 file changed

+36
-36
lines changed

1 file changed

+36
-36
lines changed

1-js/04-object-basics/02-object-copy/article.md

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,65 @@
11
# Object references and copying
22

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+
أحد الاختلافات الأساسية بين الكائنات والقيم الأساسية هو أن الكائنات تخزن وتنسخ "ضمنيًا" وبالمقابل، يتم نسخ القيم الأساسية بالكامل.
44

5-
That's easy to understand if we look a bit under the hood of what happens when we copy a value.
5+
هذا سهل الفهم إذا نظرنا قليلاً تحت الغطاء لما يحدث عند نسخ القيمة.
66

7-
Let's start with a primitive, such as a string.
7+
لنبدأ بالقيمة الأساسية، مثل السلسلة.
88

9-
Here we put a copy of `message` into `phrase`:
9+
هنا نضع نسخة من `message` في `phrase`:
1010

1111
```js
1212
let message = "Hello!";
1313
let phrase = message;
1414
```
1515

16-
As a result we have two independent variables, each one storing the string `"Hello!"`.
16+
نحن الآن لدينا متغيران مستقلان، يخزن كل منهما السلسانة `"Hello!"`.
1717

1818
![](variable-copy-value.svg)
1919

20-
Quite an obvious result, right?
20+
نتيجة واضحة جدًا، أليس كذلك؟
2121

22-
Objects are not like that.
22+
الكائنات ليست كذلك.
2323

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+
**يخزن المتغير الذي يشير إلى كائن ليس الكائن نفسه، ولكن "عنوانه في الذاكرة" - بعبارة أخرى "مرجعًا" له.**
2525

26-
Let's look at an example of such a variable:
26+
لنلقي نظرة على مثال لمتغير من هذا النوع:
2727

2828
```js
2929
let user = {
3030
name: "John",
3131
};
3232
```
3333

34-
And here's how it's actually stored in memory:
34+
وهنا كيف يتم تخزينه في الذاكرة:
3535

3636
![](variable-contains-reference.svg)
3737

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` (على اليسار) على "مرجع" إلى هذا المكان.
3939

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`، كورقة ورق بها عنوان الكائن.
4141

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 بالنظر إلى ما يوجد في ذلك العنوان وينفذ العملية على الكائن الفعلي.
4343

44-
Now here's why it's important.
44+
والآن هنا هو السبب في أهمية هذا الأمر.
4545

46-
**When an object variable is copied, the reference is copied, but the object itself is not duplicated.**
46+
**عند نسخ متغير الكائن ، يتم نسخ المرجع ، ولكن الكائن نفسه لا يتم استنساخه.**
4747

48-
For instance:
48+
على سبيل المثال:
4949

5050
```js no-beautify
5151
let user = { name: "John" };
5252

53-
let admin = user; // ينسخ المؤشر
53+
let admin = user; // المرجع يتم نسخه
5454
```
5555

56-
Now we have two variables, each storing a reference to the same object:
56+
الآن لدينا متغيران، يخزن كل منهما مرجعًا إلى نفس الكائن نفسه:
5757

5858
![](variable-copy-reference.svg)
5959

60-
As you can see, there's still one object, but now with two variables that reference it.
60+
كما يمكن رؤية، لا يوجد سوى كائن واحد، ولكن الآن بوجود متغيرين يشيران إليه.
6161

62-
We can use either variable to access the object and modify its contents:
62+
يمكننا استخدام أي من المتغيرين للوصول إلى الكائن وتعديل محتوياته:
6363

6464
```js run
6565
let user = { name: 'John' };
@@ -73,7 +73,7 @@ admin.name = 'Pete'; // تم تغييرها بواسطة المؤشر "admin"
7373
alert(*!*user.name*/!*); // 'Pete', التغيرات مرئية بواسطة مؤشر "user"
7474
```
7575
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`) في وقت لاحق، فنحن لا نفتح سوى نفس الخزانة ويمكننا الوصول إلى المحتويات المغيرة.
7777
7878
## المقارنة بالمؤشرات
7979
@@ -89,7 +89,7 @@ alert(a == b); // true, كلاهما يشيران لنفس الكائن
8989
alert(a === b); // true
9090
```
9191
92-
And here two independent objects are not equal, even though they look alike (both are empty):
92+
وهنا لا يتساوى كائنان مستقلان، على الرغم من أنهما يبدوان متشابهين (كلاهما فارغ):
9393
9494
```js run
9595
let a = {};
@@ -98,15 +98,15 @@ let b = {}; // كائنان منفصلان
9898
alert(a == b); // false
9999
```
100100
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`، يتم تحويل الكائنات إلى قيم أولية. سندرس كيفية عمل تحويلات الكائنات قريبًا جدًا، ولكن لنقل الحقيقة، تحتاج هذه المقارنات نادرًا جدًا - عادة ما تظهر نتيجة خطأ في البرمجة.
102102
103-
## Cloning and merging, Object.assign [#cloning-and-merging-object-assign]
103+
## استنساخ ودمج، Object.assign [#cloning-and-merging-object-assign]
104104
105105
نسخ المتغير ينشئ مؤشر آخر لنفس الكائن.
106106
107107
لكن ماذا إذا أردنا نسخ الكائن نفسه كنسخة منفصلة ؟
108108
109-
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+
هذا أيضًا قابل للتنفيذ، ولكنه أكثر صعوبة قليلاً، لأنه لا يوجد طريقة مدمجة لذلك في جافا سكريبت. ولكن نادرًا ما يكون هناك حاجة - فالنسخ بالإشارة جيد في معظم الأحيان.
110110
111111
لكن إذا أردنا ذلك حقًا يمكننا فعل ذلك عن طريق عمل كائن آخر والمرور على خواص الكائن الحالي ونسخها واحدة تلو الأخرى.
112112
@@ -189,9 +189,9 @@ let clone = Object.assign({}, user);
189189
190190
## النسخ المتداخل
191191
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}`، والتي سيتم تغطيتها في وقت لاحق في البرنامج التعليمي.
193193
194-
## Nested cloning
194+
## النسخ الشامل
195195
196196
مثل هذا:
197197
@@ -229,14 +229,14 @@ user.sizes.width++; // تغيير الخاصية من مكان
229229
alert(clone.sizes.width); // 51, يجعل التغيير مئي في المكان الآخر
230230
```
231231
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]` وإذا كانت كائنًا، فإنه يتم تكرار هيكله أيضًا. يُسمى ذلك "نسخًا عميقًا".
233233
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).
235235
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* be modified.
236+
````smart header="يمكن تعديل الكائنات التي تم تعريفها بـ const"
237+
آثار جانبية مهمة لتخزين الكائنات كمراجع هو أنه يمكن تعديل كائن معرف بـ `const`.
238238

239-
For instance:
239+
على سبيل المثال:
240240

241241
```js run
242242
const user = {
@@ -250,16 +250,16 @@ user.name = "Pete"; // (*)
250250
alert(user.name); // Pete
251251
```
252252

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` ثابتة، يجب أن يشير دائمًا إلى نفس الكائن، ولكن يمكن تغيير خصائص هذا الكائن.
254254

255-
In other words, the `const user` gives an error only if we try to set `user=...` as a whole.
255+
بمعنى آخر، يتم إصدار خطأ عند استخدام `const user` فقط إذا حاولنا تعيين `user=...` ككل.
256256

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>.
258258
````
259259

260260
## ملخص
261261

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+
الكائنات تتم تعيينها ونسخها بالمرجع. وبمعنى آخر، يخزن المتغير ليس "قيمة الكائن" ولكن "مرجع" (عنوان في الذاكرة) للقيمة. لذلك، عند نسخ هذا المتغير أو تمريره كوسيط لدالة، يتم نسخ هذا المرجع، وليس الكائن نفسه.
263263

264264
كل العمليات التي تتم بواسطة النسخة (مثل إضافة وحذف الخواص) تحدث على نفس الكائن.
265265

0 commit comments

Comments
 (0)