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
+29-28Lines changed: 29 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -322,77 +322,78 @@ alert(obj.__proto__); // [object Object] - the value is an object, didn't work a
322
322
323
323
## فحص الكينونة, "in" معامل
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!
325
+
ميزة ملحوظة للكائنات في الجافا اسكريبت ، مقارنة بالعديد من اللغات الأخرى ، هي أنه من الممكن الوصول إلى أي خاصية. لن يكون هناك خطأ إذا كانت الخاصية غير موجودة!
326
326
327
-
Reading a non-existing property just returns `undefined`. So we can easily test whether the property exists:
327
+
328
+
قراءة خاصية غير موجودة تُرجع فقط "غير محدد". لذا يمكننا بسهولة اختبار ما إذا كانت الخاصية موجودة:
328
329
329
330
```js run
330
331
let user = {};
331
332
332
-
alert( user.noSuchProperty===undefined ); //true means "no such property"
333
+
alert( user.noSuchProperty===undefined ); //"تحقق هذه الموازنة يشير إلى "عدم وجود الخاصية
333
334
```
334
335
335
-
There's also a special operator `"in"`for that.
336
+
يوجد أيضا مُعامل خاصة `"in"`لفحص تواجد أي خاصية.
336
337
337
338
The syntax is:
338
339
```js
339
340
"key"in object
340
341
```
341
342
342
-
For instance:
343
+
مثلاً:
343
344
344
345
```js run
345
346
let user = { name:"John", age:30 };
346
347
347
-
alert( "age"in user ); // true, user.age exists
348
-
alert( "blabla"in user ); // false, user.blabla doesn't exist
348
+
alert( "age"in user ); // true, user.age موجود
349
+
alert( "blabla"in user ); // false, user.blabla غير موجود
349
350
```
350
351
351
-
Please note that on the left side of `in`there must be a *property name*. That's usually a quoted string.
352
+
يرجى ملاحظة أنه في الجهة اليسرى من `in`يجب أن يكون هناك *اسم خاصية*. يكون عادة نصًا بين علامتي تنصيص.
352
353
353
-
If we omit quotes, that means a variable, it should contain the actual name to be tested. For instance:
354
+
إذا حذفنا علامات التنصيص ، فهذا يعني متغيرًا ، يجب أن يحتوي على الاسم الفعلي المراد اختباره. على سبيل المثال:
354
355
355
356
```js run
356
357
let user = { age:30 };
357
358
358
359
let key ="age";
359
-
alert( *!*key*/!* in user ); //true, property"age"exists
360
+
alert( *!*key*/!* in user ); //true, خاصية"age"موجودة
360
361
```
361
362
362
-
Why does the`in` operator exist? Isn't it enough to compare against`undefined`?
363
+
لماذا يوجد المعامل`in`? أليس من الكافي المقارنة مقابل`undefined`?
363
364
364
-
Well, most of the time the comparison with `undefined`works fine. But there's a special case when it fails, but`"in"`works correctly.
365
+
حسناً, في معظم الأحيان المقارنة ب `undefined`تكون جيدة. ولكن هناك حالة خاصة عندما تفشل, لكن`"in"`تعمل بشكل صحيح.
365
366
366
-
It's when an object property exists, but stores`undefined`:
367
+
يحدث ذلك عند وجود خاصية داخل كائن ، ولكنها تخزن قيمة`undefined`:
367
368
368
369
```js run
369
370
let obj = {
370
371
test:undefined
371
372
};
372
373
373
-
alert( obj.test ); //it's undefined, so - no such property?
374
+
alert( obj.test ); //تعطي undefined, لذلك - ألا توجد هذه الخاصية؟
374
375
375
-
alert( "test"in obj ); // true, the property does exist!
376
+
alert( "test"in obj ); // true, الخاصية موجودة بالفعل!
376
377
```
378
+
379
+
في الكود السابق, الخاصية `obj.test` موجودة بالفعل. لذا معامل `in`يعمل بشكل صحيح.
377
380
378
-
In the code above, the property `obj.test` technically exists. So the `in` operator works right.
379
-
380
-
Situations like this happen very rarely, because `undefined` should not be explicitly assigned. We mostly use `null` for "unknown" or "empty" values. So the `in` operator is an exotic guest in the code.
381
+
مواقف كهذه تحدث نادراً, لأن `undefined` لا ينبغي تعيينها بشكل ذاتي. عادة ما نستخدم `null` ل "unknown" أو "empty" قيم . لذا معامل `in` يعتبر ضيفاً غريباً في الكود.
381
382
382
383
383
384
## The "for..in" loop
384
385
385
-
To walk over all keys of an object, there exists a special form of the loop: `for..in`. This is a completely different thing from the `for(;;)` construct that we studied before.
386
+
للمرور على كل مفاتيح الكائن, يوجد شكل خاص آخر للحلقة loop: `for..in`. هذه الحلقة مختلفة تمامًا عما درسناه سابقًا، أي الحلقة `for(;;)`.
386
387
387
-
The syntax:
388
+
طريقة الكتابة في الكود:
388
389
389
390
```js
390
391
for (key in object) {
391
-
// executes the body for each key among object properties
392
+
//يتنفذ ما بداخل الحلقة لكل مفتاح ضمن خاصيات الكائن
392
393
}
393
394
```
394
395
395
-
For instance, let's output all properties of`user`:
396
+
مثلاً, لنطبع جميع خاصيات الكائن`user`:
396
397
397
398
```js run
398
399
let user = {
@@ -402,20 +403,20 @@ let user = {
402
403
};
403
404
404
405
for (let key in user) {
405
-
//keys
406
+
//المفاتيح
406
407
alert( key ); // name, age, isAdmin
407
-
//values for the keys
408
+
//قيم المفاتيح
408
409
alert( user[key] ); // John, 30, true
409
410
}
410
411
```
411
412
412
-
Note that all "for" constructs allow us to declare the looping variable inside the loop, like `let key`here.
413
+
لاحظ أن جميع تراكيب "for" تتيح لنا تعريف متغير التكرار بِداخل الحلقة، مثل `let key`في المثال السابق,.
413
414
414
-
Also, we could use another variable name here instead of `key`. For instance, `"for (let prop in obj)"`is also widely used.
415
+
أيضاً, يمكننا استخدام اسم متغير آخر بدلا من `key`. مثلا, `"for (let prop in obj)"`مستخدم أيضاَ بكثرة.
415
416
416
-
### Ordered like an object
417
+
### الترتيب مثل الكائنات
417
418
418
-
Are objects ordered? In other words, if we loop over an object, do we get all properties in the same order they were added? Can we rely on this?
419
+
هل الكائنات مرتبة? بمعنى آخر, إن تنقلنا في حلقة خلال كائن, هل نحصل على جميع الخاصيات بنفس الترتيب الذي أُضيفت به؟ وهل يمكننا الاعتماد على هذا؟
419
420
420
421
The short answer is: "ordered in a special fashion": integer properties are sorted, others appear in creation order. The details follow.
0 commit comments