work
This commit is contained in:
parent
1bffa43db4
commit
057783d216
373 changed files with 203 additions and 190 deletions
|
@ -0,0 +1,43 @@
|
|||
**Error**!
|
||||
|
||||
Try it:
|
||||
|
||||
```js run
|
||||
let user = {
|
||||
name: "John",
|
||||
go: function() { alert(this.name) }
|
||||
}
|
||||
|
||||
(user.go)() // error!
|
||||
```
|
||||
|
||||
The error message in most browsers does not give understanding what went wrong.
|
||||
|
||||
**The error appears because a semicolon is missing after `user = {...}`.**
|
||||
|
||||
Javascript does not assume a semicolon before a bracket `(user.go)()`, so it reads the code like:
|
||||
|
||||
```js no-beautify
|
||||
let user = { go:... }(user.go)()
|
||||
```
|
||||
|
||||
Then we can also see that such a joint expression is syntactically a call of the object `{ go: ... }` as a function with the argument `(user.go)`. And that also happens on the same line with `let user`, so the `user` object has not yet even been defined, hence the error.
|
||||
|
||||
If we insert the semicolon, all is fine:
|
||||
|
||||
```js run
|
||||
let user = {
|
||||
name: "John",
|
||||
go: function() { alert(this.name) }
|
||||
}*!*;*/!*
|
||||
|
||||
(user.go)() // John
|
||||
```
|
||||
|
||||
Please note that brackets around `(user.go)` do nothing here. Usually they setup the order of operations, but here the dot `.` works first anyway, so there's no effect. Only the semicolon thing matters.
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
19
1-js/3-object-basics/3-object-methods/2-check-syntax/task.md
Normal file
19
1-js/3-object-basics/3-object-methods/2-check-syntax/task.md
Normal file
|
@ -0,0 +1,19 @@
|
|||
importance: 2
|
||||
|
||||
---
|
||||
|
||||
# Syntax check
|
||||
|
||||
What is the resule of this code?
|
||||
|
||||
|
||||
```js no-beautify
|
||||
let user = {
|
||||
name: "John",
|
||||
go: function() { alert(this.name) }
|
||||
}
|
||||
|
||||
(user.go)()
|
||||
```
|
||||
|
||||
P.S. There's a pitfall :)
|
Loading…
Add table
Add a link
Reference in a new issue