Merge branch 'master' into patch-47

This commit is contained in:
Ilya Kantor 2019-10-07 04:46:50 +03:00 committed by GitHub
commit c0d8fdbfff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 16 additions and 16 deletions

View file

@ -72,8 +72,8 @@ Why so?
That's for historical reasons. That's for historical reasons.
- The `"prototype"` property of a constructor function has worked since very ancient times. - The `"prototype"` property of a constructor function has worked since very ancient times.
- Later, in the year 2012: `Object.create` appeared in the standard. It gave the ability to create objects with a given prototype, but did not provide the ability to get/set it. So browsers implemented the non-standard `__proto__` accessor that allowed the user to get/set a prototype at any time. - Later, in the year 2012, `Object.create` appeared in the standard. It gave the ability to create objects with a given prototype, but did not provide the ability to get/set it. So browsers implemented the non-standard `__proto__` accessor that allowed the user to get/set a prototype at any time.
- Later, in the year 2015: `Object.setPrototypeOf` and `Object.getPrototypeOf` were added to the standard, to perform the same functionality as `__proto__`. As `__proto__` was de-facto implemented everywhere, it was kind-of deprecated and made its way to the Annex B of the standard, that is: optional for non-browser environments. - Later, in the year 2015, `Object.setPrototypeOf` and `Object.getPrototypeOf` were added to the standard, to perform the same functionality as `__proto__`. As `__proto__` was de-facto implemented everywhere, it was kind-of deprecated and made its way to the Annex B of the standard, that is: optional for non-browser environments.
As of now we have all these ways at our disposal. As of now we have all these ways at our disposal.

View file

@ -192,7 +192,7 @@ class Rabbit extends Animal {}
alert(Rabbit.__proto__ === Animal); // true alert(Rabbit.__proto__ === Animal); // true
// for regular methods // for regular methods
alert(Rabbit.prototype.__proto__ === Animal.prototype); alert(Rabbit.prototype.__proto__ === Animal.prototype); // true
``` ```
## Summary ## Summary

View file

@ -21,7 +21,7 @@ alert(filteredArr); // 10, 50
alert(filteredArr.isEmpty()); // false alert(filteredArr.isEmpty()); // false
``` ```
Please note a very interesting thing. Built-in methods like `filter`, `map` and others -- return new objects of exactly the inherited type `PowerArray`. Their internal implementation uses object `constructor` property for that. Please note a very interesting thing. Built-in methods like `filter`, `map` and others -- return new objects of exactly the inherited type `PowerArray`. Their internal implementation uses the object's `constructor` property for that.
In the example above, In the example above,
```js ```js
@ -74,7 +74,7 @@ Built-in objects have their own static methods, for instance `Object.keys`, `Arr
As we already know, native classes extend each other. For instance, `Array` extends `Object`. As we already know, native classes extend each other. For instance, `Array` extends `Object`.
Normally, when one class extends another, both static and non-static methods are inherited. That was thoroughly explained in the chapter [](info:static-properties-methods#statics-and-inheritance). Normally, when one class extends another, both static and non-static methods are inherited. That was thoroughly explained in the article [](info:static-properties-methods#statics-and-inheritance).
But built-in classes are an exception. They don't inherit statics from each other. But built-in classes are an exception. They don't inherit statics from each other.

View file

@ -1,6 +1,6 @@
# Browser environment, specs # Browser environment, specs
The JavaScript language was initially created for web browsers. Since then, it has evolved and become a language with many uses and platforms. The JavaScript language was initially created for web browsers. Since then it has evolved and become a language with many uses and platforms.
A platform may be a browser, or a web-server or another *host*, even a coffee machine. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*. A platform may be a browser, or a web-server or another *host*, even a coffee machine. Each of them provides platform-specific functionality. The JavaScript specification calls that a *host environment*.
@ -60,14 +60,14 @@ For instance, server-side scripts that download HTML pages and process them can
``` ```
```smart header="CSSOM for styling" ```smart header="CSSOM for styling"
CSS rules and stylesheets are structured in a different way than HTML. There's a separate specification [CSSOM](https://www.w3.org/TR/cssom-1/) that explains how they are represented as objects, and how to read and write them. CSS rules and stylesheets are structured in a different way than HTML. There's a separate specification, [CSS Object Model (CSSOM)](https://www.w3.org/TR/cssom-1/), that explains how they are represented as objects, and how to read and write them.
CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because usually CSS rules are static. We rarely need to add/remove CSS rules from JavaScript, but that's also possible. CSSOM is used together with DOM when we modify style rules for the document. In practice though, CSSOM is rarely required, because usually CSS rules are static. We rarely need to add/remove CSS rules from JavaScript, but that's also possible.
``` ```
## BOM (Browser object model) ## BOM (Browser Object Model)
Browser Object Model (BOM) are additional objects provided by the browser (host environment) to work with everything except the document. The Browser Object Model (BOM) represents additional objects provided by the browser (host environment) for working with everything except the document.
For instance: For instance:

View file

@ -132,9 +132,9 @@ To make the document unscrollable, it's enough to set `document.body.style.overf
```online ```online
Try it: Try it:
<button onclick="document.body.style.overflow = 'hidden'">`document.body.style.overflow = 'hidden'`</button> <button onclick="document.body.style.overflow = 'hidden'">document.body.style.overflow = 'hidden'</button>
<button onclick="document.body.style.overflow = ''">`document.body.style.overflow = ''`</button> <button onclick="document.body.style.overflow = ''">document.body.style.overflow = ''</button>
The first button freezes the scroll, the second one resumes it. The first button freezes the scroll, the second one resumes it.
``` ```

View file

@ -22,7 +22,7 @@ That policy is called "CORS": Cross-Origin Resource Sharing.
## Why CORS is needed? A brief history ## Why CORS is needed? A brief history
CORS exists protect the internet from evil hackers. CORS exists to protect the internet from evil hackers.
Seriously. Let's make a very brief historical digression. Seriously. Let's make a very brief historical digression.
@ -149,7 +149,7 @@ As you can see, `Origin` header contains exactly the origin (domain/protocol/por
The server can inspect the `Origin` and, if it agrees to accept such a request, adds a special header `Access-Control-Allow-Origin` to the response. That header should contain the allowed origin (in our case `https://javascript.info`), or a star `*`. Then the response is successful, otherwise an error. The server can inspect the `Origin` and, if it agrees to accept such a request, adds a special header `Access-Control-Allow-Origin` to the response. That header should contain the allowed origin (in our case `https://javascript.info`), or a star `*`. Then the response is successful, otherwise an error.
The browser plays the role of a trusted mediator here: The browser plays the role of a trusted mediator here:
1. It ensures that the corrent `Origin` is sent with a cross-origin request. 1. It ensures that the correct `Origin` is sent with a cross-origin request.
2. It checks for permitting `Access-Control-Allow-Origin` in the response, if it exists, then JavaScript is allowed to access the response, otherwise it fails with an error. 2. It checks for permitting `Access-Control-Allow-Origin` in the response, if it exists, then JavaScript is allowed to access the response, otherwise it fails with an error.
![](xhr-another-domain.svg) ![](xhr-another-domain.svg)
@ -203,13 +203,13 @@ With such `Access-Control-Expose-Headers` header, the script is allowed to read
We can use any HTTP-method: not just `GET/POST`, but also `PATCH`, `DELETE` and others. We can use any HTTP-method: not just `GET/POST`, but also `PATCH`, `DELETE` and others.
Some time ago no one could even assume that a webpage is able to do such requests. So there may exist webservices that treat a non-standard method as a signal: "That's not a browser". They can take it into account when checking access rights. Some time ago no one could even imagine that a webpage could make such requests. So there may still exist webservices that treat a non-standard method as a signal: "That's not a browser". They can take it into account when checking access rights.
So, to avoid misunderstandings, any "non-simple" request -- that couldn't be done in the old times, the browser does not make such requests right away. Before it sends a preliminary, so-called "preflight" request, asking for permission. So, to avoid misunderstandings, any "non-simple" request -- that couldn't be done in the old times, the browser does not make such requests right away. Before it sends a preliminary, so-called "preflight" request, asking for permission.
A preflight request uses method `OPTIONS`, no body and two headers: A preflight request uses method `OPTIONS`, no body and two headers:
- `Access-Control-Request-Method` header has the method of an the non-simple request. - `Access-Control-Request-Method` header has the method of the non-simple request.
- `Access-Control-Request-Headers` header provides a comma-separated list of its non-simple HTTP-headers. - `Access-Control-Request-Headers` header provides a comma-separated list of its non-simple HTTP-headers.
If the server agrees to serve the requests, then it should respond with empty body, status 200 and headers: If the server agrees to serve the requests, then it should respond with empty body, status 200 and headers:
@ -273,7 +273,7 @@ Access-Control-Allow-Headers: API-Key,Content-Type,If-Modified-Since,Cache-Contr
Access-Control-Max-Age: 86400 Access-Control-Max-Age: 86400
``` ```
Now the browser can see that `PATCH` in `Access-Control-Allow-Methods` and `Content-Type,API-Key` are in the list `Access-Control-Allow-Headers`, so it sends out the main request. Now the browser can see that `PATCH` is in `Access-Control-Allow-Methods` and `Content-Type,API-Key` are in the list `Access-Control-Allow-Headers`, so it sends out the main request.
Besides, the preflight response is cached for time, specified by `Access-Control-Max-Age` header (86400 seconds, one day), so subsequent requests will not cause a preflight. Assuming that they fit the cached allowances, they will be sent directly. Besides, the preflight response is cached for time, specified by `Access-Control-Max-Age` header (86400 seconds, one day), so subsequent requests will not cause a preflight. Assuming that they fit the cached allowances, they will be sent directly.