Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork3
Revert "Object methods, "this""#149
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
46 changes: 23 additions & 23 deletions1-js/04-object-basics/04-object-methods/4-object-property-this/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,55 @@ | ||
**Answer: an error.** | ||
Try it: | ||
```js run | ||
functionmakeUser() { | ||
return { | ||
name: "John", | ||
ref: this | ||
}; | ||
} | ||
letuser =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. | ||
Here the value of`this`inside `makeUser()`is`undefined`,because it is called as a function, not as a method with "dot" syntax. | ||
The value of`this`is one for the whole function, code blocks and object literals do not affect it. | ||
So `ref: this`actually takes current`this`of the function. | ||
We can rewrite the function and return the same`this`with`undefined` value: | ||
```js run | ||
functionmakeUser(){ | ||
return this; //this time there's no object literal | ||
} | ||
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. | ||
Here's the opposite case: | ||
```js run | ||
functionmakeUser() { | ||
return { | ||
name: "John", | ||
*!* | ||
ref() { | ||
return this; | ||
} | ||
*/!* | ||
}; | ||
} | ||
letuser =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 `.`. |
16 changes: 8 additions & 8 deletions1-js/04-object-basics/04-object-methods/4-object-property-this/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions1-js/04-object-basics/04-object-methods/7-calculator/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
21 changes: 11 additions & 10 deletions1-js/04-object-basics/04-object-methods/7-calculator/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
14 changes: 7 additions & 7 deletions1-js/04-object-basics/04-object-methods/7-calculator/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,21 @@ | ||
```js run demo solution | ||
letcalculator = { | ||
sum() { | ||
return this.a + this.b; | ||
}, | ||
mul() { | ||
return this.a * this.b; | ||
}, | ||
read() { | ||
this.a = +prompt('a?', 0); | ||
this.b = +prompt('b?', 0); | ||
} | ||
}; | ||
calculator.read(); | ||
alert(calculator.sum() ); | ||
alert(calculator.mul() ); | ||
``` |
20 changes: 10 additions & 10 deletions1-js/04-object-basics/04-object-methods/7-calculator/task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
17 changes: 6 additions & 11 deletions1-js/04-object-basics/04-object-methods/8-chain-calls/_js.view/solution.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
letladder = { | ||
step: 0, | ||
up: function() { | ||
this.step++; | ||
return this; | ||
}, | ||
down: function() { | ||
this.step--; | ||
return this; | ||
}, | ||
showStep: function() { | ||
alert(this.step); | ||
return this; | ||
} | ||
}; |
30 changes: 15 additions & 15 deletions1-js/04-object-basics/04-object-methods/8-chain-calls/_js.view/test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
36 changes: 18 additions & 18 deletions1-js/04-object-basics/04-object-methods/8-chain-calls/solution.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,39 @@ | ||
The solution is to return the object itself from every call. | ||
```js run demo | ||
letladder = { | ||
step: 0, | ||
up() { | ||
this.step++; | ||
*!* | ||
return this; | ||
*/!* | ||
}, | ||
down() { | ||
this.step--; | ||
*!* | ||
return this; | ||
*/!* | ||
}, | ||
showStep() { | ||
alert( this.step ); | ||
*!* | ||
return this; | ||
*/!* | ||
} | ||
}; | ||
ladder.up().up().down().showStep().down().showStep(); //shows 1 then 0 | ||
``` | ||
We also can write a single call per line. For long chains it's more readable: | ||
```js | ||
ladder | ||
.up() | ||
.up() | ||
.down() | ||
.showStep() // 1 | ||
.down() | ||
.showStep(); // 0 | ||
``` |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.