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/04-object-basics/04-object-methods/2-check-syntax/solution.md
+2-8
Original file line number
Diff line number
Diff line change
@@ -15,13 +15,13 @@ The error message in most browsers does not give understanding what went wrong.
15
15
16
16
**The error appears because a semicolon is missing after `user = {...}`.**
17
17
18
-
JavaScript does not assume a semicolon before a bracket `(user.go)()`, so it reads the code like:
18
+
JavaScript does not auto-insert a semicolon before a bracket `(user.go)()`, so it reads the code like:
19
19
20
20
```js no-beautify
21
21
let user = { go:... }(user.go)()
22
22
```
23
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.
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
25
26
26
If we insert the semicolon, all is fine:
27
27
@@ -35,9 +35,3 @@ let user = {
35
35
```
36
36
37
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.
That's because rules that set `this` do not look at object literals.
17
+
That's because rules that set `this` do not look at object definition. Only the moment of call matters.
18
18
19
-
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method.
19
+
Here the value of `this` inside `makeUser()` is `undefined`, because it is called as a function, not as a method with "dot" syntax.
20
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.
21
+
The value of `this` is one for the whole function, code blocks and object literals do not affect it.
22
22
23
23
So `ref: this` actually takes current `this` of the function.
24
24
@@ -42,5 +42,3 @@ alert( user.ref().name ); // John
42
42
```
43
43
44
44
Now it works, because `user.ref()` is a method. And the value of `this` is set to the object before dot `.`.
Copy file name to clipboardExpand all lines: 1-js/04-object-basics/04-object-methods/article.md
+6-4
Original file line number
Diff line number
Diff line change
@@ -257,11 +257,11 @@ user.hi(); // John (the simple call works)
257
257
*/!*
258
258
```
259
259
260
-
On the last line there is a ternary operator that chooses either `user.hi` or `user.bye`. In this case the result is `user.hi`.
260
+
On the last line there is a conditinal operator that chooses either `user.hi` or `user.bye`. In this case the result is `user.hi`.
261
261
262
-
The method is immediately called with parentheses `()`. But it doesn't work right!
262
+
Then the method is immediately called with parentheses `()`. But it doesn't work right!
263
263
264
-
You can see that the call results in an error, because the value of `"this"` inside the call becomes `undefined`.
264
+
As you can see, the call results in an error, because the value of `"this"` inside the call becomes `undefined`.
265
265
266
266
This works (object dot method):
267
267
```js
@@ -306,7 +306,7 @@ The Reference Type is a "specification type". We can't explicitly use it, but it
306
306
The value of Reference Type is a three-value combination `(base, name, strict)`, where:
307
307
308
308
- `base` is the object.
309
-
- `name` is the property.
309
+
- `name` is the property name.
310
310
- `strict` is true if `use strict` is in effect.
311
311
312
312
The result of a property access `user.hi` is not a function, but a value of Reference Type. For `user.hi` in strict mode it is:
@@ -318,6 +318,8 @@ The result of a property access `user.hi` is not a function, but a value of Refe
318
318
319
319
When parentheses `()` are called on the Reference Type, they receive the full information about the object and its method, and can set the right `this` (`=user` in this case).
320
320
321
+
Reference type is a special "intermediary" internal type, with the purpose to pass information from dot `.` to calling parentheses `()`.
322
+
321
323
Any other operation like assignment `hi = user.hi` discards the reference type as a whole, takes the value of `user.hi` (a function) and passes it on. So any further operation "loses" `this`.
322
324
323
325
So, as the result, the value of `this` is only passed the right way if the function is called directly using a dot `obj.method()` or square brackets `obj['method']()` syntax (they do the same here). Later in this tutorial, we will learn various ways to solve this problem such as [func.bind()](/bind#solution-2-bind).
Copy file name to clipboardExpand all lines: 5-network/06-url/article.md
+30-21
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
4
4
The built-in [URL](https://url.spec.whatwg.org/#api) class provides a convenient interface for creating and parsing URLs.
5
5
6
-
We don't have to use it at all. There are no networking methods that require exactly an `URL` object, strings are good enough. But sometimes it can be really helpful.
6
+
There are no networking methods that require exactly an `URL` object, strings are good enough. So technically we don't have to use `URL`. But sometimes it can be really helpful.
7
7
8
8
## Creating an URL
9
9
@@ -13,8 +13,28 @@ The syntax to create a new URL object:
13
13
newURL(url, [base])
14
14
```
15
15
16
-
-**`url`** -- the text url
17
-
-**`base`** -- an optional base for the `url`
16
+
-**`url`** -- the URL string or path (if base is set, see below).
17
+
-**`base`** -- an optional base, if set and `url` has only path, then the URL is generated relative to `base`.
18
+
19
+
For example, these two URLs are same:
20
+
21
+
```js run
22
+
let url1 =newURL('https://javascript.info/profile/admin');
23
+
let url2 =newURL('/profile/admin', 'https://javascript.info');
```smart header="We can use `URL` everywhere instead of a string"
52
61
We can use an `URL` object in `fetch` or `XMLHttpRequest`, almost everywhere where a string url is expected.
53
62
54
63
In the vast majority of methods it's automatically converted to a string.
55
64
```
56
65
57
-
## SearchParams
66
+
## SearchParams "?..."
58
67
59
-
Let's say we want to create an url with given search params, for instance, `https://google.com/search?query=value`.
68
+
Let's say we want to create an url with given search params, for instance, `https://google.com/search?query=JavaScript`.
60
69
61
-
They must be correctly encoded.
70
+
They must be correctly encoded to include non-latin charcters, spaces etc.
62
71
63
-
In very old browsers, before `URL` appeared, we'd use built-in functions `encodeURIComponent/decodeURIComponent`.
72
+
Some time ago, before `URL` objects appeared, we'd use built-in functions `encodeURIComponent/decodeURIComponent`. They have some problems, but now that doesn't matter.
64
73
65
-
Now, there's no need: `url.searchParams` is an object of type [URLSearchParams](https://url.spec.whatwg.org/#urlsearchparams).
74
+
There's URL property for that: `url.searchParams` is an object of type [URLSearchParams](https://url.spec.whatwg.org/#urlsearchparams).
66
75
67
76
It provides convenient methods for search parameters:
68
77
@@ -81,7 +90,7 @@ For example:
81
90
82
91
```js run
83
92
let url = new URL('https://google.com/search');
84
-
url.searchParams.set('query', 'test me!');
93
+
url.searchParams.set('query', 'test me!'); // added parameter with a space and !
0 commit comments