Skip to content

Prototype methods, objects without proto #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
يمكن للدالة أن تأخذ كل الخصائص المعدودة (enumerable) باستخدام `Object.keys` وطباعة قائمة بهم.

The method can take all enumerable keys using `Object.keys` and output their list.

To make `toString` non-enumerable, let's define it using a property descriptor. The syntax of `Object.create` allows us to provide an object with property descriptors as the second argument.
لجعل الدالة `toString` غير معدودة (non-enumerable)، سنقوم بتعريفها باستخدام واصف (descriptor). ويسمح لنا شكل `Object.create` أن نضع واصفًا لخاصية كمتغير ثانٍ.

```js run
*!*
let dictionary = Object.create(null, {
toString: { // define toString property
value() { // the value is a function
toString: {
value() { // قيمتها عبارة عن دالة
return Object.keys(this).join();
}
}
Expand All @@ -17,15 +16,16 @@ let dictionary = Object.create(null, {
dictionary.apple = "Apple";
dictionary.__proto__ = "test";

// apple and __proto__ is in the loop
// apple و __proto__ فى التكرار
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
}
alert(key); // "apple", ثم "__proto__"
}

// comma-separated list of properties by toString
// قائمة من االخصائص مفصول بينها بالفاصلة
alert(dictionary); // "apple,__proto__"
```

When we create a property using a descriptor, its flags are `false` by default. So in the code above, `dictionary.toString` is non-enumerable.
عند إنشاء خاصية بواصف فإن مُعرِّفاتها تكون قيمها `false`. ولذلك فى الكود أعلاه فإن `dictionary.toString` هي غير معدودة (non-enumerable).

See the the chapter [](info:property-descriptors) for review.
أنظر فصل [](info:property-descriptors) للمراجعة.
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
importance: 5
درجة الأهمية: 5

---

# Add toString to the dictionary
# إضافة الدالة toString إلى القاموس

There's an object `dictionary`, created as `Object.create(null)`, to store any `key/value` pairs.
يوجد كائن يسمي `dictionary`، تم إنشاؤه باستخدام `Object.create(null)` لتخزين خصائص بقيمها.

Add method `dictionary.toString()` into it, that should return a comma-delimited list of keys. Your `toString` should not show up in `for..in` over the object.
أضف الدالة `dictionary.toString()` لهذا الكائن والتى يجب أن تقوم بإرجاع قائمة من الخصائص بينها الفاصلة. هذا الدالة يجب أن لا تظهر فى التكرار `for..in`.

Here's how it should work:
هنا كيف سيتم استخدامها:

```js
let dictionary = Object.create(null);

*!*
// your code to add dictionary.toString method
// الكود الخاص بك لإنشاء الدالة dictionary.toString
*/!*

// add some data
// أضف بعض البيانات
dictionary.apple = "Apple";
dictionary.__proto__ = "test"; // __proto__ is a regular property key here
dictionary.__proto__ = "test"; // __proto__ هي خاصية عادية

// only apple and __proto__ are in the loop
// تظهر فقط apple & __proto__
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "apple", ثم "__proto__"
}

// your toString in action
// استخدام الدالة toString التى صنعتها
alert(dictionary); // "apple,__proto__"
```
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

The first call has `this == rabbit`, the other ones have `this` equal to `Rabbit.prototype`, because it's actually the object before the dot.
الإستدعاء الأول تكون `this == rabbit` والآخرين تكون قيمة `this` مساوية لـ `Rabbit.prototype` وذلك لأنه الكائن قبل النقطة.

So only the first call shows `Rabbit`, other ones show `undefined`:
وبالتالى فإن أول استدعاء فقط يعرض `Rabbit` والباقى يعرضون `undefined`:

```js run
function Rabbit(name) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 5
درجة الأهمية: 5

---

# The difference between calls
# الفرق بين الإستدعاءات

Let's create a new `rabbit` object:
هيا نقوم بإنشاء كائن جديد يسمي `rabbit`:

```js
function Rabbit(name) {
Expand All @@ -17,7 +17,7 @@ Rabbit.prototype.sayHi = function() {
let rabbit = new Rabbit("Rabbit");
```

These calls do the same thing or not?
هل هذه الإستدعاءات تقوم بنفس الوظيفة أم لا؟

```js
rabbit.sayHi();
Expand Down
Loading