Skip to content

Commit f2332b1

Browse files
authored
Merge pull request #122 from mahdiHashemi14/master
Object methods, "this"
2 parents 028d144 + a17a2ce commit f2332b1

File tree

6 files changed

+105
-107
lines changed

6 files changed

+105
-107
lines changed

1-js/04-object-basics/04-object-methods/4-object-property-this/solution.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
**Answer: an error.**
1+
**جواب: یک ارور**
22

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

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

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

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

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

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

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

3232
alert( makeUser().name ); // Error: Cannot read property 'name' of undefined
3333
```
34-
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.
34+
همانطور که می‌بینید نتیجه `alert( makeUser().name )` با نتیجه `alert( user.ref.name )` از مثال قبل یکسان است.
3535

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

3838
```js run
3939
function makeUser() {
@@ -52,4 +52,4 @@ let user = makeUser();
5252
alert( user.ref().name ); // John
5353
```
5454

55-
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
55+
حالا کار می‌کند، چون `user.ref()` یک متد است. مقدار `this` برابر با شیء قبل از نقطه `.` است.

1-js/04-object-basics/04-object-methods/4-object-property-this/task.md

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

33
---
44

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

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

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

1111
```js
1212
function makeUser() {
@@ -18,6 +18,6 @@ function makeUser() {
1818

1919
let user = makeUser();
2020

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

1-js/04-object-basics/04-object-methods/7-calculator/task.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,22 @@ importance: 5
22

33
---
44

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

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

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

1313
```js
1414
let calculator = {
15-
// ... your code ...
15+
// ... کد شما ...
1616
};
1717

1818
calculator.read();
1919
alert( calculator.sum() );
2020
alert( calculator.mul() );
2121
```
2222

23-
[demo]
24-
23+
[دمو]

1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
The solution is to return the object itself from every call.
1+
راه حل این است که خود شیء را با هربار صدازدن برگردانیم.
22

33
```js run
44
let ladder = {
@@ -26,7 +26,7 @@ let ladder = {
2626
ladder.up().up().down().up().down().showStep(); // 1
2727
```
2828

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

3131
```js
3232
ladder
@@ -37,4 +37,3 @@ ladder
3737
.down()
3838
.showStep(); // 1
3939
```
40-

1-js/04-object-basics/04-object-methods/8-chain-calls/task.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ importance: 2
22

33
---
44

5-
# Chaining
5+
# زنجیره‌ای
66

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

99
```js
1010
let ladder = {
@@ -15,13 +15,13 @@ let ladder = {
1515
down() {
1616
this.step--;
1717
},
18-
showStep: function() { // shows the current step
18+
showStep: function() { // قدم کنونی را نشان می‌دهد
1919
alert( this.step );
2020
}
2121
};
2222
```
2323

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

2626
```js
2727
ladder.up();
@@ -30,10 +30,10 @@ ladder.down();
3030
ladder.showStep(); // 1
3131
```
3232

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

3535
```js
3636
ladder.up().up().down().showStep(); // 1
3737
```
3838

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

0 commit comments

Comments
 (0)