Skip to content

Prototype methods, objects without __proto__ #174

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 6 commits into from
Nov 22, 2021
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,31 +1,31 @@

The method can take all enumerable keys using `Object.keys` and output their list.
این روش می‌تواند همه کلیدهای شمارش‌پذیر را با استفاده از `Object.keys` گرفته و فهرست آنها را خروجی کند.

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`، بیایید آن را با استفاده از یک توصیفگر مشخص کنیم. سینتکس `Object.create` به ما اجازه می‌دهد تا یک شیء را با توصیفگرهای ویژگی به عنوان آرگومان دوم ارائه کنیم.

```js run
*!*
let dictionary = Object.create(null, {
toString: { // define toString property
value() { // the value is a function
toString: { // toString تعریف ویژگی
value() { // مقدار یک تابع است
return Object.keys(this).join();
}
}
});
*/!*

dictionary.apple = "Apple";
dictionary.__proto__ = "test";
dictionary.apple = "سیب";
dictionary.__proto__ = "تست";

// apple and __proto__ is in the loop
// در حلقه هستند __proto__ و apple
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "__proto__" سپس ،"apple"
}

// comma-separated list of properties by toString
// 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` غیرقابل شمارش است.

See the the chapter [](info:property-descriptors) for review.
برای بررسی به فصل [](info:property-descriptors) مراجعه کنید.
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ importance: 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)` ایجاد شده است تا هر جفت `key/value` را ذخیره کند.

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()` را به آن اضافه کنید، که باید فهرستی از کلیدها با کاما را برگرداند. `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.apple = "سیب";
dictionary.__proto__ = "تست"; // یک ویژگی معمولی است __proto__ در اینجا

// only apple and __proto__ are in the loop
// در حلقه وجود دارند __proto__ تنها سیب و
for(let key in dictionary) {
alert(key); // "apple", then "__proto__"
alert(key); // "__proto__" سپس ،"apple"
}

// your toString in action
alert(dictionary); // "apple,__proto__"
// شما در عمل toString
alert(dictionary); // "سیب,__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`:
بنابراین فقط اولین تماس `خرگوش` را نشان می‌دهد، سایر تماس‌ها `undefined` را نشان می‌دهند:

```js run
function Rabbit(name) {
Expand All @@ -11,9 +11,9 @@ Rabbit.prototype.sayHi = function() {
alert( this.name );
}

let rabbit = new Rabbit("Rabbit");
let rabbit = new Rabbit("خرگوش");

rabbit.sayHi(); // Rabbit
rabbit.sayHi(); // خرگوش
Rabbit.prototype.sayHi(); // undefined
Object.getPrototypeOf(rabbit).sayHi(); // undefined
rabbit.__proto__.sayHi(); // undefined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 5

---

# The difference between calls
# تفاوت بین فراخوانی‌ها

Let's create a new `rabbit` object:
بیایید یک شیء `rabbit` جدید ایجاد کنیم:

```js
function Rabbit(name) {
Expand All @@ -14,10 +14,10 @@ Rabbit.prototype.sayHi = function() {
alert(this.name);
};

let rabbit = new Rabbit("Rabbit");
let rabbit = new Rabbit("خرگوش");
```

These calls do the same thing or not?
این فراخوانی‌ها همین کار را می‌کنند یا نه؟

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