This commit is contained in:
Ilya Kantor 2017-03-25 10:06:45 +03:00
parent c0a4565b60
commit 627ea67c8e
2 changed files with 43 additions and 7 deletions

View file

@ -18,14 +18,14 @@ When JavaScript was created, it initially had another name: "LiveScript". But Ja
But as it evolved, JavaScript became a fully independent language, with its own specification called [ECMAScript](http://en.wikipedia.org/wiki/ECMAScript), and now it has no relation to Java at all. But as it evolved, JavaScript became a fully independent language, with its own specification called [ECMAScript](http://en.wikipedia.org/wiki/ECMAScript), and now it has no relation to Java at all.
``` ```
At present, JavaScript can execute not only in the browser, but also on the server, or actually on any device where exists a special program called [the JavaScript engine](https://en.wikipedia.org/wiki/JavaScript_engine). At present, JavaScript can execute not only in the browser, but also on the server, or actually on any device where exists a special program called [the JavaScript engine](https://en.wikipedia.org/wiki/JavaScript_engine).
The browser has an embedded engine, sometimes it's also called a "JavaScript virtual machine". The browser has an embedded engine, sometimes it's also called a "JavaScript virtual machine".
Different engines have different "codenames", for example: Different engines have different "codenames", for example:
- [V8]("https://en.wikipedia.org/wiki/V8_(JavaScript_engine)") -- in Chrome and Opera. - [V8](https://en.wikipedia.org/wiki/V8_(JavaScript_engine)) -- in Chrome and Opera.
- [Gecko]("https://en.wikipedia.org/wiki/Gecko_(software)") -- in Firefox. - [Gecko](https://en.wikipedia.org/wiki/Gecko_(software)) -- in Firefox.
- ...There are other codenames like "Trident", "Chakra" for different versions of IE, "Nitro" and "SquirrelFish" for Safari etc. - ...There are other codenames like "Trident", "Chakra" for different versions of IE, "Nitro" and "SquirrelFish" for Safari etc.
These terms above are good to remember, because they are used in developer articles in the internet. We'll use them too. For instance, if "a feature X is supported by V8", then it probably works in Chrome and Opera. These terms above are good to remember, because they are used in developer articles in the internet. We'll use them too. For instance, if "a feature X is supported by V8", then it probably works in Chrome and Opera.
@ -35,7 +35,7 @@ These terms above are good to remember, because they are used in developer artic
Engines are complicated. But the basics are easy. Engines are complicated. But the basics are easy.
1. The script is written and distributed as a plain text (can be compressed/optimized by so-called "javascript minifiers"). 1. The script is written and distributed as a plain text (can be compressed/optimized by so-called "javascript minifiers").
2. The engine (embedded if it's a browser) reads the script ("parses") and converts ("compiles") it to the machine language. 2. The engine (embedded if it's a browser) reads the script ("parses") and converts ("compiles") it to the machine language.
3. And then it runs, pretty fast. 3. And then it runs, pretty fast.
The engine applies optimizations on every stage of the process. It even watches the script as it runs, analyzes the data which flows through it and applies optimizations to the machine-code basing on that knowledge. The engine applies optimizations on every stage of the process. It even watches the script as it runs, analyzes the data which flows through it and applies optimizations to the machine-code basing on that knowledge.
@ -104,7 +104,7 @@ That's normal, because projects and requirements are different for everyone.
So recently a plethora of new languages appeared, which are *transpiled* (converted) to JavaScript before they run in the browser. So recently a plethora of new languages appeared, which are *transpiled* (converted) to JavaScript before they run in the browser.
The modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language, autoconverting it "under the hood". The modern tools make the transpilation very fast and transparent, actually allowing developers to code in another language, autoconverting it "under the hood".
Examples of such languages: Examples of such languages:
@ -119,4 +119,3 @@ There are more. Of course even if we use one of those languages, we should also
- JavaScript was initially created as a browser-only language, but now used in many other environments as well. - JavaScript was initially created as a browser-only language, but now used in many other environments as well.
- At this moment, JavaScript as a unique position as a most widely adopted browser language with full integration with HTML/CSS. - At this moment, JavaScript as a unique position as a most widely adopted browser language with full integration with HTML/CSS.
- There are over languages that get "transpiled" to JavaScript and provide certain features. It is recommended to take a look at them, at least briefly, after mastering JavaScript. - There are over languages that get "transpiled" to JavaScript and provide certain features. It is recommended to take a look at them, at least briefly, after mastering JavaScript.

View file

@ -538,6 +538,43 @@ alert( a == b ); // false
For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj == 5`, objects are converted to primitives. We'll study how object conversions work very soon, but to say the truth, such comparisons are necessary very rarely and usually are a result of a coding mistake. For comparisons like `obj1 > obj2` or for a comparison against a primitive `obj == 5`, objects are converted to primitives. We'll study how object conversions work very soon, but to say the truth, such comparisons are necessary very rarely and usually are a result of a coding mistake.
### Const object
An object declared as `const` *can* be changed.
For instance:
```js run
const user = {
name: "John"
};
*!*
user.age = 25; // (*)
*/!*
alert(user.age); // 25
```
It might seem that the line `(*)` would cause an error, but no, there's totally no problem. That's because `const` fixes the value of `user` itself. And here `user` stores the reference to the same object all the time. The line `(*)` goes *inside* the object, it doesn't reassign `user`.
The `const` would give an error if we try to set `user` to something else, for instance:
```js run
const user = {
name: "John"
};
*!*
// Error (can't reassign user)
*/!*
user = {
name: "Pete"
};
```
...But what if we want to make constant object properties? So that `user.age = 25` would give an error. That's possible too. We'll cover it in the chapter <info:property-flags-descriptors>.
## Cloning and merging, Object.assign ## Cloning and merging, Object.assign
So, copying an object variable creates one more reference to the same object. So, copying an object variable creates one more reference to the same object.
@ -661,7 +698,7 @@ alert(clone.sizes.width); // 51, see the result from the other one
To fix that, we should use the cloning loop that examines each value of `user[key]` and, if it's an object, then replicate it's structure as well. That is called a "deep cloning". To fix that, we should use the cloning loop that examines each value of `user[key]` and, if it's an object, then replicate it's structure as well. That is called a "deep cloning".
There's a standard algorithm for deep cloning that handles the case above and more complex cases, called the [Structured cloning algorithm](w3c.github.io/html/infrastructure.html#internal-structured-cloning-algorithm). Not to reinvent the wheel, we can use a working implementation of it from the JavaScript library [lodash](https://lodash.com), the method is called [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep). There's a standard algorithm for deep cloning that handles the case above and more complex cases, called the [Structured cloning algorithm](https://w3c.github.io/html/infrastructure.html#internal-structured-cloning-algorithm). Not to reinvent the wheel, we can use a working implementation of it from the JavaScript library [lodash](https://lodash.com), the method is called [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep).