You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/2-first-steps/18-function-parameters/article.md
+10-7Lines changed: 10 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -119,13 +119,16 @@ The `...rest` must always be the last.
119
119
120
120
````smart header="The `arguments` variable"
121
121
122
-
In old times, there were no rest operator. But there was a special variable named `arguments` that contained all arguments by their index. It is still supported and can be used like this:
122
+
In old times, there was no rest operator. But there is a special variable named `arguments` that contains all arguments by their index. It is still supported and can be used like this:
123
123
124
124
```js run
125
125
function showName() {
126
126
alert( arguments[0] );
127
127
alert( arguments[1] );
128
128
alert( arguments.length );
129
+
130
+
// for..of works too
131
+
// for(let arg of arguments) alert(arg);
129
132
}
130
133
131
134
// shows: Julius, Caesar, 2
@@ -135,7 +138,7 @@ showName("Julius", "Caesar");
135
138
showName("Ilya");
136
139
```
137
140
138
-
The downside is that `arguments` looks like an array, but it's not. It does not support many useful array features. It mainly exists for backwards compatibility, usually the rest operator is better.
141
+
The downside is that though `arguments` looks like an array, but it's not. It does not support many useful array methods that we'll study later, and if they're needed, then the rest operator should be used.
The error message in most browsers does not give understanding what went wrong.
15
+
16
+
**The error appears because a semicolon is missing after `user = {...}`.**
17
+
18
+
Javascript does not assume a semicolon before a bracket `(user.go)()`, so it reads the code like:
19
+
20
+
```js no-beautify
21
+
let user = { go:... }(user.go)()
22
+
```
23
+
24
+
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.
25
+
26
+
If we insert the semicolon, all is fine:
27
+
28
+
```js run
29
+
let user = {
30
+
name:"John",
31
+
go:function() { alert(this.name) }
32
+
}*!*;*/!*
33
+
34
+
(user.go)() // John
35
+
```
36
+
37
+
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.
2. The same, brackets do not change the order of operations here, the dot is first anyway.
7
+
8
+
3. Here we have a more complex call `(expression).method()`. The call works as if it were split into two lines:
9
+
10
+
```js no-beautify
11
+
f =obj.go; // calculate the expression
12
+
f(); // call what we have
13
+
```
14
+
15
+
Here `f()` is executed as a function, without `this`.
16
+
17
+
4. The similar thing as `(3)`, to the left of the dot `.` we have an expression.
18
+
19
+
To explain the behavior of `(3)` and `(4)` we need to recall that property accessors (dot or square brackets) return a value of the Reference Type.
20
+
21
+
Any operation on it except a method call (like assignment `=` or `||`) turns it into an ordinary value, which does not carry the information allowing to set `this`.
That's because rules that set `this` do not look at object literals.
18
+
19
+
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method.
20
+
21
+
And the object literal itself has no effect on `this`. The value of `this` is one for the whole function, code blocks and object literals do not affect it.
22
+
23
+
So `ref: this` actually takes current `this` of the function.
24
+
25
+
Here's the opposite case:
26
+
27
+
```js run
28
+
functionmakeUser() {
29
+
return {
30
+
name:"John",
31
+
*!*
32
+
ref() {
33
+
returnthis;
34
+
}
35
+
*/!*
36
+
};
37
+
};
38
+
39
+
let user =makeUser();
40
+
41
+
alert( user.ref().name ); // John
42
+
```
43
+
44
+
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
0 commit comments