Skip to content

Commit 41803f8

Browse files
author
[first commit]
committed
translate methods of primitives into AR
1 parent 7a4c4e9 commit 41803f8

3 files changed

Lines changed: 59 additions & 55 deletions

File tree

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
Try running it:
2+
جرب تشغيلها:
33

44
```js run
55
let str = "Hello";
@@ -9,16 +9,17 @@ str.test = 5; // (*)
99
alert(str.test);
1010
```
1111

12-
Depending on whether you have `use strict` or not, the result may be:
12+
يعتمد على ما إذا كان لديك `use strict` أم لا, وقد تكون النتيجة كما يلي:
1313
1. `undefined` (no strict mode)
1414
2. An error (strict mode).
1515

16-
Why? Let's replay what's happening at line `(*)`:
16+
لماذا؟ لنعيد ما يحدث على السطر `(*)`:
1717

18-
1. When a property of `str` is accessed, a "wrapper object" is created.
19-
2. In strict mode, writing into it is an error.
20-
3. Otherwise, the operation with the property is carried on, the object gets the `test` property, but after that the "wrapper object" disappears, so in the last line `str` has no trace of the property.
18+
1. عندما تكون خاصية ال `str` يمكن الولوج إليها, "wrapper object" يكون قد تكون.
19+
2. في حالة ال strict mode, الكتابة فيها يكون خطأ.
20+
3. فيما عَدا ذلك, تجري العملية المتعلقة بالخصائص, ال object يحصل علي خاصية `test` , و لكن بعد ذلك يختفي "wrapper object" , لذلك في السطر الأخير `str` لا أثر له.
2121

22-
**This example clearly shows that primitives are not objects.**
22+
**وهذا المثال يبين بوضوح أن الأنواع البدائية ليست objects.**
2323

24-
They can't store additional data.
24+
لا يمكنهم تخزين بيانات إضافية
25+
.

1-js/05-data-types/01-primitives-methods/1-string-new-property/task.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ importance: 5
22

33
---
44

5-
# Can I add a string property?
5+
# هل يمكن أن أضيف خاصية نصية؟
66

77

8-
Consider the following code:
8+
انظر الي الكود التالي:
99

1010
```js
1111
let str = "Hello";
@@ -15,4 +15,5 @@ str.test = 5;
1515
alert(str.test);
1616
```
1717

18-
How do you think, will it work? What will be shown?
18+
كيف تعتقد أنها ستنجح؟
19+
ماذا سيتضح؟
Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
# Methods of primitives
1+
# الأساليب البدائية للبيانات
22

3-
JavaScript allows us to work with primitives (strings, numbers, etc.) as if they were objects. They also provide methods to call as such. We will study those soon, but first we'll see how it works because, of course, primitives are not objects (and here we will make it even clearer).
3+
الجافاسكربت تتيح لنا العمل بالأساليب البدائية (strings, numbers, etc.) كما لو كانت objects. كما أنها توفر وسائل للأتصال علي هذا النحو. وسندرسها قريبا, لكن أولاً سنرى كيف يعمل لأن الأساليب البدائية ليست objects
4+
(وهنا سنجعله أكثر وضوحاً).
45

5-
Let's look at the key distinctions between primitives and objects.
6+
لننظر إلى الفروق الرئيسية بين الأساليب البدائية و objects .
67

7-
A primitive
8+
بدائية
89

9-
- Is a value of a primitive type.
10-
- There are 7 primitive types: `string`, `number`, `bigint`, `boolean`, `symbol`, `null` and `undefined`.
10+
- هو قيمة من النوع البدائي.
11+
- هناك 7 أنواع بدائية: `string`, `number`, `bigint`, `boolean`, `symbol`, `null` أيضا `undefined`.
1112

1213
An object
1314

14-
- Is capable of storing multiple values as properties.
15-
- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript: functions, for example, are objects.
15+
- قادر على تخزين قيم متعددة كخصائص.
16+
- يمكن تكوينه بواسطة `{}`, على سبيل المثال: `{name: "John", age: 30}`. هناك أنواع أخرى من objects في الجافاسكربت: functions, على سبيل المثال, objects.
1617

17-
One of the best things about objects is that we can store a function as one of its properties.
18+
أحد أفضل الأشياء في ال objects هو أنه يمكننا أن نخزن a function كأحد خصائصها.
1819

1920
```js run
2021
let john = {
@@ -27,74 +28,75 @@ let john = {
2728
john.sayHi(); // Hi buddy!
2829
```
2930

30-
So here we've made an object `john` with the method `sayHi`.
31+
إذاً هنا صنعنا an object `john` مع ال `sayHi` method.
3132

32-
Many built-in objects already exist, such as those that work with dates, errors, HTML elements, etc. They have different properties and methods.
33+
العديد من ال built-in objects موجودة بالفعل, مثل تلك التي تعمل مع التواريخ و الأخطاء و عناصر HTML , إلخ. فلهم خصائص وأساليب مختلفة.
3334

34-
But, these features come with a cost!
35+
ولكن ، تأتي هذه الميزات بتكلفة!
3536

36-
Objects are "heavier" than primitives. They require additional resources to support the internal machinery.
37+
Objects "اثقل" من الأساليب البدائية. وهي تتطلب موارد إضافية لدعم الآلية الداخلية.
3738

38-
## A primitive as an object
39+
## بدائي كا an object
3940

40-
Here's the paradox faced by the creator of JavaScript:
41+
هنا التناقض الذي واجه صانع جافاسكربت:
4142

42-
- There are many things one would want to do with a primitive like a string or a number. It would be great to access them as methods.
43-
- Primitives must be as fast and lightweight as possible.
43+
- هناك العديد من الأشياء التي يمكن أن يفعلها الشخص بالأسلوب البدائي مثل string أو number. سيكون من الرائع استخدامهم كا methods.
44+
- الأساليب البدائية يجب أن تكون سريعة وخفيفة بقدر الإمكان.
4445

45-
The solution looks a little bit awkward, but here it is:
46+
إن الحل يبدو غريبا بعض الشيء، ولكن ها هو:
4647

47-
1. Primitives are still primitive. A single value, as desired.
48-
2. The language allows access to methods and properties of strings, numbers, booleans and symbols.
49-
3. In order for that to work, a special "object wrapper" that provides the extra functionality is created, and then is destroyed.
48+
1. الأساليب البدائية ما زالت بدائية. قيمة واحدة, كما يحلو لها.
49+
2. تسمح اللغة بالوصول الي ال methods و الخصائص الخاصة ب strings, numbers, booleans و symbols.
50+
3. ولكي يعمل ذلك، يتم إنشاء "object wrapper" خاص يوفر الوظائف الإضافية، ثم يتم تدميره.
5051

51-
The "object wrappers" are different for each primitive type and are called: `String`, `Number`, `Boolean` and `Symbol`. Thus, they provide different sets of methods.
52+
ال "object wrappers" تختلف لكل نوع بدائي و تدعى: `String`, `Number`, `Boolean` و `Symbol`. وبالتالي، فإنها توفر مجموعات مختلفة من methods.
5253

53-
For instance, there exists a string method [str.toUpperCase()](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) that returns a capitalized `str`.
54+
على سبيل المثال
55+
, هنالك a string method [()str.toUpperCase](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/toUpperCase) التي ترجع capitalized `str`.
5456

55-
Here's how it works:
57+
إليك كيف يعمل:
5658

5759
```js run
5860
let str = "Hello";
5961

6062
alert( str.toUpperCase() ); // HELLO
6163
```
6264

63-
Simple, right? Here's what actually happens in `str.toUpperCase()`:
65+
بسيط، أليس كذلك؟ إليكم ما يحدث في الواقع `()str.toUpperCase`:
6466

65-
1. The string `str` is a primitive. So in the moment of accessing its property, a special object is created that knows the value of the string, and has useful methods, like `toUpperCase()`.
66-
2. That method runs and returns a new string (shown by `alert`).
67-
3. The special object is destroyed, leaving the primitive `str` alone.
67+
1. النص `str` هو نوع بدائي. لذا في لحظة الولوج لخصائصه, يتكون object خاص يعرف قيمة النص, و لديه methods مفيدة, مثل `()toUpperCase`.
68+
2. هذه ال method تعْملُ وتقوم بإرجاع نص جديد (يعرض بواسطة `alert`).
69+
3. ال object تم تدميره, و ترك النوع البدائي `str` مُنْفَرِداً.
6870

69-
So primitives can provide methods, but they still remain lightweight.
71+
يمكن للأنواع البدائية أن توفر methods, لكنها لا تزال خفيفة.
7072

71-
The JavaScript engine highly optimizes this process. It may even skip the creation of the extra object at all. But it must still adhere to the specification and behave as if it creates one.
73+
محرك الجافاسكربت يرتقي بهذه العملية إلى أقصى حد. قد يتخطي عملية تكوين object إضافي على الإطلاق. ولكن ما زال عليها أن تلتزم بالمواصفات وتتصرف كما لو أنها تنشئ واحد.
7274

73-
A number has methods of its own, for instance, [toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) rounds the number to the given precision:
75+
الرقم لديه ال methods الخاصة به, علي سبيل المثال, [toFixed(n)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed) تقوم بتقريب الرقم حسب القيمة المعطاه :
7476

7577
```js run
7678
let n = 1.23456;
7779

7880
alert( n.toFixed(2) ); // 1.23
7981
```
8082

81-
We'll see more specific methods in chapters <info:number> and <info:string>.
83+
سنري methods أكثر تحديداً في الفصول <info:number> و <info:string>.
8284

8385

84-
````warn header="Constructors `String/Number/Boolean` are for internal use only"
85-
Some languages like Java allow us to explicitly create "wrapper objects" for primitives using a syntax like `new Number(1)` or `new Boolean(false)`.
86+
````warn header="Constructors `String/Number/Boolean` هي للاستعمال الداخلي فقط"
87+
بعض اللغات مثل جافا تسمح لنا بصريح التعبير بصنع "object wrappers" للأنواع البدائية بإستخدام syntax مثل `new Number(1)` أو `new Boolean(false)`.
8688

87-
In JavaScript, that's also possible for historical reasons, but highly **unrecommended**. Things will go crazy in several places.
89+
في الجافاسكربت, وهذا ممكن أيضا لأسباب تاريخية, اكن **غير موصى به**. ستصاب الأمور بالجنون في عدة مواضع.
8890

89-
For instance:
91+
على سبيل المثال:
9092

9193
```js run
9294
alert( typeof 0 ); // "number"
9395

9496
alert( typeof new Number(0) ); // "object"!
9597
```
9698

97-
Objects are always truthy in `if`, so here the alert will show up:
99+
Objects دائما صادق في `if`, لذا هنا سيظهر الإنذار:
98100

99101
```js run
100102
let zero = new Number(0);
@@ -104,25 +106,25 @@ if (zero) { // zero is true, because it's an object
104106
}
105107
```
106108

107-
On the other hand, using the same functions `String/Number/Boolean` without `new` is a totally sane and useful thing. They convert a value to the corresponding type: to a string, a number, or a boolean (primitive).
109+
من ناحية أخرى, استخدام نفس ال functions `String/Number/Boolean` بدون `new` هو شيء سَليم و مفيد. يحولون قيمة إلى النوع المقابل: إلي a string, a number, or a boolean (primitive - نوع بدائي).
108110

109-
For example, this is entirely valid:
111+
علي سبيل المثال, هذا صحيح تماما:
110112
```js
111113
let num = Number("123"); // convert a string to number
112114
```
113115
````
114116
115117
116118
````warn header="null/undefined have no methods"
117-
The special primitives `null` and `undefined` are exceptions. They have no corresponding "wrapper objects" and provide no methods. In a sense, they are "the most primitive".
119+
الأنواع البدائية الخاصة `null` و `undefined` حالات استثنائية. ليس لديهم ما يشابههم "wrapper objects" و لا تزودنا بال methods. بمعنى, هم "الأكثر بدائية".
118120
119-
An attempt to access a property of such value would give the error:
121+
ومن شأن محاولة الوصول إلى خاصية بهذه القيمة أن تعطي هذا الخطأ:
120122
121123
```js run
122124
alert(null.test); // error
123125
````
124126

125-
## Summary
127+
## مُلخّص
126128

127-
- Primitives except `null` and `undefined` provide many helpful methods. We will study those in the upcoming chapters.
128-
- Formally, these methods work via temporary objects, but JavaScript engines are well tuned to optimize that internally, so they are not expensive to call.
129+
- الأنواع البدائية ماعدا `null` و `undefined` تزودنا بالعديد من ال methods المساعدة. وسندرسها في الفصول القادمة.
130+
- رَسمِيّا, هذه ال methods تعمل عن طريق objects مؤقتة, ولكن محركات جافاسكربت مضبوطة بشكل جيد لتحسين ذلك داخليا, لذا ليس مكلفا إستدعائهم.

0 commit comments

Comments
 (0)