Skip to content

Object methods, "this" #122

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 23 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
cbce3a6
Translate a part of "article.md"
mahdiHash May 9, 2021
06bfc0f
Translate a part of "article.md"
mahdiHash May 9, 2021
df42688
Translate a part of "article.md"
mahdiHash May 9, 2021
d957aab
Translate a part of "article.md"
mahdiHash May 10, 2021
70763af
Translate a part of "article.md"
mahdiHash May 10, 2021
2c505c1
Translate a part of "article.md"
mahdiHash May 11, 2021
ba78e90
Translate a part of "article.md"
mahdiHash May 11, 2021
1b0f2e7
Translate "article.md"
mahdiHash May 12, 2021
636f8f8
Translate "summary"
mahdiHash May 12, 2021
b8c1d8d
Translate "object property this" task
mahdiHash May 12, 2021
010cea2
Translate "object property this" solution
mahdiHash May 12, 2021
62cce35
Translate "calculator" task
mahdiHash May 12, 2021
a692106
Translate "chain calls" task
mahdiHash May 12, 2021
3c648d3
Translate "chain calls" solution
mahdiHash May 12, 2021
5c48b27
Update 1-js/04-object-basics/04-object-methods/7-calculator/task.md
mahdiHash May 12, 2021
1cd8587
Update 1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md
mahdiHash May 12, 2021
cad4331
Update 1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md
mahdiHash May 12, 2021
b51bb94
Update 1-js/04-object-basics/04-object-methods/8-chain-calls/task.md
mahdiHash May 12, 2021
ccffded
Update 1-js/04-object-basics/04-object-methods/article.md
mahdiHash May 12, 2021
14e6dd9
Update 1-js/04-object-basics/04-object-methods/article.md
mahdiHash May 12, 2021
08e4ef5
Update 1-js/04-object-basics/04-object-methods/article.md
mahdiHash May 12, 2021
7c89baf
Update 1-js/04-object-basics/04-object-methods/article.md
mahdiHash May 12, 2021
a17a2ce
Update 1-js/04-object-basics/04-object-methods/article.md
mahdiHash May 12, 2021
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,6 +1,6 @@
**Answer: an error.**
**جواب: یک ارور**

Try it:
آن را امتحان کنید:
```js run
function makeUser() {
return {
Expand All @@ -14,26 +14,26 @@ let user = makeUser();
alert( user.ref.name ); // Error: Cannot read property 'name' of undefined
```

That's because rules that set `this` do not look at object definition. Only the moment of call matters.
دلیلش این است که قواعدی که `this` را تشکیل می‌دهند به تعریف شیء نگاه نمی‌کنند. فقط لحظه‌ی صدازدن مهم است.

Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
اینجا مقدار `this` درون `makeUser()` برابر با `undefined` است، چون به عنوان تابع صدا زده شده است نه به عنوان یک متد با سینتکس نقطه.

The value of `this` is one for the whole function, code blocks and object literals do not affect it.
مقدار `this` برای تمام تابع یکی است و بلوک‌های کد و شیءهای لیترال روی آن تاثیری نمی‌گذارند.

So `ref: this` actually takes current `this` of the function.
بنابراین `ref: this` در واقع `this` کنونی تابع را می‌گیرد.

We can rewrite the function and return the same `this` with `undefined` value:
ما می‌توانیم تابع را بازنویسی کنیم و `this` یکسان را با مقدار `undefined` برگردانیم:

```js run
function makeUser(){
return this; // this time there's no object literal
return this; // این بار هیچ شیء لیترالی وجود ندارد
}

alert( makeUser().name ); // Error: Cannot read property 'name' of undefined
```
As you can see the result of `alert( makeUser().name )` is the same as the result of `alert( user.ref.name )` from the previous example.
همانطور که می‌بینید نتیجه `alert( makeUser().name )` با نتیجه `alert( user.ref.name )` از مثال قبل یکسان است.

Here's the opposite case:
کد پایین متضاد قبلی است:

```js run
function makeUser() {
Expand All @@ -52,4 +52,4 @@ let user = makeUser();
alert( user.ref().name ); // John
```

Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
حالا کار می‌کند، چون `user.ref()` یک متد است. مقدار `this` برابر با شیء قبل از نقطه `.` است.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ importance: 5

---

# Using "this" in object literal
# استفاده از "this" در شیء لیترال

Here the function `makeUser` returns an object.
در اینجا تابع `makeUser` یک شیء را برمی‌گرداند.

What is the result of accessing its `ref`? Why?
نتیجه دسترسی داشتن به `ref` چیست؟ چرا؟

```js
function makeUser() {
Expand All @@ -18,6 +18,6 @@ function makeUser() {

let user = makeUser();

alert( user.ref.name ); // What's the result?
alert( user.ref.name ); // نتیجه چیست؟
```

15 changes: 7 additions & 8 deletions 1-js/04-object-basics/04-object-methods/7-calculator/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ importance: 5

---

# Create a calculator
# یک ماشین‌حساب بسازید

Create an object `calculator` with three methods:
یک شیء `calculator` با سه متد بسازید:

- `read()` prompts for two values and saves them as object properties.
- `sum()` returns the sum of saved values.
- `mul()` multiplies saved values and returns the result.
- `read()` برای دو مقدار prompt می‌کند و آنها را به عنوان ویژگی‌های شیء ذخیره می‌کند.
- `sum()` مجموع مقدارهای ذخیره‌شده را برمی‌گرداند.
- `mul()` مقدارهای ذخیره‌شده را ضرب می‌کند و نتیجه را برمی‌گرداند.

```js
let calculator = {
// ... your code ...
// ... کد شما ...
};

calculator.read();
alert( calculator.sum() );
alert( calculator.mul() );
```

[demo]

[دمو]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The solution is to return the object itself from every call.
راه حل این است که خود شیء را با هربار صدازدن برگردانیم.

```js run
let ladder = {
Expand Down Expand Up @@ -26,7 +26,7 @@ let ladder = {
ladder.up().up().down().up().down().showStep(); // 1
```

We also can write a single call per line. For long chains it's more readable:
همچنین می‌توانیم به ازای هر خط یک بار صدا بزنیم. برای زنجیره‌های طولانی این روش خوانایی بیشتری دارد:

```js
ladder
Expand All @@ -37,4 +37,3 @@ ladder
.down()
.showStep(); // 1
```

12 changes: 6 additions & 6 deletions 1-js/04-object-basics/04-object-methods/8-chain-calls/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 2

---

# Chaining
# زنجیره‌ای

There's a `ladder` object that allows to go up and down:
یک شیء `ladder` وجود دارد که بالا و پایین رفتن را ممکن می‌کند:

```js
let ladder = {
Expand All @@ -15,13 +15,13 @@ let ladder = {
down() {
this.step--;
},
showStep: function() { // shows the current step
showStep: function() { // قدم کنونی را نشان می‌دهد
alert( this.step );
}
};
```

Now, if we need to make several calls in sequence, can do it like this:
حال اگر ما نیاز داشته باشیم که برای چند بار متوالی صدا بزنیم، می‌توانیم اینگونه این کار را انجام دهیم:

```js
ladder.up();
Expand All @@ -30,10 +30,10 @@ ladder.down();
ladder.showStep(); // 1
```

Modify the code of `up`, `down` and `showStep` to make the calls chainable, like this:
کد `up`، `down` و `showStep` را تغییر دهید تا صدازدن‌ها را زنجیره‌ای کنید، مثل این:

```js
ladder.up().up().down().showStep(); // 1
```

Such approach is widely used across JavaScript libraries.
چنین روشی در بین کتابخانه‌های جاوااسکریپت به طور گسترده استفاده می‌شود.
Loading