This commit is contained in:
Ilya Kantor 2017-03-19 17:09:37 +03:00
parent e2443e8de6
commit 75e30539ef
73 changed files with 195 additions and 195 deletions

View file

@ -1,9 +1,9 @@
# Objects
As we know, there are 7 language types in Javascript. Six of them are called "primitive", because their values contain only a single thing (be it a string or a number or whatever).
As we know, there are 7 language types in JavaScript. Six of them are called "primitive", because their values contain only a single thing (be it a string or a number or whatever).
In contrast, objects are used to store keyed collections of various data and more complex entities. In Javascript, objects penetrate almost every aspect of the language. So we must understand them first before going in-depth anywhere else.
In contrast, objects are used to store keyed collections of various data and more complex entities. In JavaScript, objects penetrate almost every aspect of the language. So we must understand them first before going in-depth anywhere else.
[cut]
@ -544,7 +544,7 @@ So, copying an object variable creates one more reference to the same object.
But what if we need to duplicate an object? Create an independant copy, a clone?
That's also doable, but a little bit more difficult, because there's no built-in method for that in Javascript. Actually, that's rarely needed, copying by reference is good most of the time.
That's also doable, but a little bit more difficult, because there's no built-in method for that in JavaScript. Actually, that's rarely needed, copying by reference is good most of the time.
But if we really want that, then we need to create a new object and replicate the structure of the existing one by iterating over its properties and copying them on the primitive level.
@ -661,7 +661,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".
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](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).
@ -688,7 +688,7 @@ To make a "real copy" (a clone) we can use `Object.assign` or [_.cloneDeep(obj)
What we've studied in this chapter is called a "plain object", or just `Object`.
There are many other kinds of objects in Javascript:
There are many other kinds of objects in JavaScript:
- `Array` to store ordered data collections,
- `Date` to store the information about the date and time,

View file

@ -1,14 +1,14 @@
# Garbage collection
Memory management in Javascript is performed automatically and invisibly to us. We create primitives, objects, functions... All that takes memory.
Memory management in JavaScript is performed automatically and invisibly to us. We create primitives, objects, functions... All that takes memory.
What happens when something is not needed any more? How Javascript engine discovers that and cleans up?
What happens when something is not needed any more? How JavaScript engine discovers that and cleans up?
[cut]
## Reachability
The main concept of memory management in Javascript is *reachability*.
The main concept of memory management in JavaScript is *reachability*.
Simply put, "reachable" values are those that are accessible or useable somehow. They are guaranteed to be stored in memory.
@ -27,7 +27,7 @@ Simply put, "reachable" values are those that are accessible or useable somehow.
For instance, if there's an object in a local variable, and that object has a property referencing another object, that object is considered reachable. And those that it references -- are also reachable. Detailed examples to follow.
There's a background process in the Javascript engine that is called [garbage collector](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)). It monitors all objects and removes those that became unreachable.
There's a background process in the JavaScript engine that is called [garbage collector](https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)). It monitors all objects and removes those that became unreachable.
## A simple example
@ -185,7 +185,7 @@ Now the objects that could not be visited in the process are considered unreacha
That's the concept how garbage collection works.
Javascript engines apply many optimizations to make it run faster and not affect the execution.
JavaScript engines apply many optimizations to make it run faster and not affect the execution.
Some of the optimizations:

View file

@ -31,7 +31,7 @@ alert(id1 == id2); // false
*/!*
```
If you are familiar with Ruby or another language that also has some sort of "symbols" -- please don't be misguided. Javascript symbols are different.
If you are familiar with Ruby or another language that also has some sort of "symbols" -- please don't be misguided. JavaScript symbols are different.
## "Hidden" properties
@ -48,7 +48,7 @@ user[id] = "ID Value";
alert( user[id] ); // we can access the data using the symbol as the key
```
Now let's imagine that another script wants to have his own "id" property inside `user`, for his own purposes. That may be another Javascript library, so the scripts are completely unaware for each other.
Now let's imagine that another script wants to have his own "id" property inside `user`, for his own purposes. That may be another JavaScript library, so the scripts are completely unaware for each other.
No problem. It can create its own `Symbol("id")`.
@ -178,7 +178,7 @@ Symbols inside the registry are called *global symbols*. If we want an applicati
```smart header="That sounds like Ruby"
In some programming languages, like Ruby, there's a single symbol per name.
In Javascript, as we can see, that's right for global symbols.
In JavaScript, as we can see, that's right for global symbols.
```
### Symbol.keyFor
@ -210,7 +210,7 @@ For non-global symbols, the name is only used for debugging purposes.
## System symbols
There exist many "system" symbols that Javascript uses internally, and we can use them to fine-tune various aspects of our objects.
There exist many "system" symbols that JavaScript uses internally, and we can use them to fine-tune various aspects of our objects.
They are listed in the specification in the [Well-known symbols](https://tc39.github.io/ecma262/#sec-well-known-symbols) table:
@ -231,7 +231,7 @@ Other symbols will also become familiar when we study the corresponding language
- Symbols are useful if we want to create a field that only those who know the symbol can access.
- Symbols don't appear in `for..in` loops.
- Symbols created with `Symbol(name)` are always different, even if they have the same name. If we want same-named symbols to be equal, then we should use the global registry: `Symbol.for(name)` returns (creates if needed) a global symbol with the given name. Multiple calls return the same symbol.
- There are system symbols used by Javascript and accessible as `Symbol.*`. We can use them to alter some built-in behaviors.
- There are system symbols used by JavaScript and accessible as `Symbol.*`. We can use them to alter some built-in behaviors.
Technically, symbols are not 100% hidden. There is a build-in method [Object.getOwnPropertySymbols(obj)](mdn:js/Object/getOwnPropertySymbols) that allows to get all symbols. Also there is a method named [Reflect.ownKeys(obj)](mdn:js/Reflect/ownKeys) that returns *all* keys of an object including symbolic ones. So they are not really hidden.

View file

@ -15,7 +15,7 @@ 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:
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)()

View file

@ -36,4 +36,4 @@ Modify the code of `up` and `down` to make the calls chainable, like this:
ladder.up().up().down().showStep(); // 1
```
Such approach is widely used across Javascript libraries.
Such approach is widely used across JavaScript libraries.

View file

@ -11,7 +11,7 @@ let user = {
And, in the real world, a user can `act`: to select something from the shopping cart, to login, to logout etc.
Let's implement the same in Javascript using functions in properties.
Let's implement the same in JavaScript using functions in properties.
[cut]
@ -164,7 +164,7 @@ If we used `this.name` instead of `user.name` inside the `alert`, then the code
## "this" is not bound
In Javascript, "this" keyword behaves unlike most other programming languages. First, it can be used in any function.
In JavaScript, "this" keyword behaves unlike most other programming languages. First, it can be used in any function.
There's no syntax error in the code like that:
@ -227,7 +227,7 @@ Here we are not to judge whether this language design decision is good or bad. W
## Internals: Reference Type
```warn header="In-depth language feature"
This section covers advanced topic that may interest those who want to know Javascript better.
This section covers advanced topic that may interest those who want to know JavaScript better.
If you want to go on faster, it can be skipped or postponed.
```
@ -279,7 +279,7 @@ hi(); // Error, because this is undefined
That's because a function is a value of its own. It does not carry the object. So `hi = user.hi` saves it into the variable, and then on the last line it is completely standalone.
**To make `user.hi()` calls work, Javascript uses a trick -- the dot `'.'` returns not a function, but a value of the special [Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type).**
**To make `user.hi()` calls work, JavaScript uses a trick -- the dot `'.'` returns not a function, but a value of the special [Reference Type](https://tc39.github.io/ecma262/#sec-reference-specification-type).**
The Reference Type is a "specification type". We can't explicitly use it, but it is used internally by the language.

View file

@ -24,7 +24,7 @@ Still, there are still valid reasons why we should know how to-primitive convers
- Simple object-as-string output (`alert` and alike) is useable sometimes.
- Many built-in objects implement their own to-primitive conversion, we need to know how to work with that.
- Sometimes an unexpected conversion happens, and we should understand what's going on.
- The final one. There are quizzes and questions on interviews that rely on that knowledge. Looks like people think it's a good sign that person understands Javascript if he knows type conversions well.
- The final one. There are quizzes and questions on interviews that rely on that knowledge. Looks like people think it's a good sign that person understands JavaScript if he knows type conversions well.
## ToPrimitive
@ -81,7 +81,7 @@ When a built-in function (like `alert` or mathematical functions) or an operator
Please note -- there are only three hints. That simple. There is no "boolean" hint (all objects are `true` in boolean context) or anything else. And if we treat `"default"` and `"number"` the same, like most built-ins do, then there are only two conversions.
**To do the conversion, Javascript tries to find and call three object methods:**
**To do the conversion, JavaScript tries to find and call three object methods:**
1. Call `obj[Symbol.toPrimitive](hint)` if the method exists,
2. Otherwise if hint is `"string"`
@ -126,7 +126,7 @@ As we can see from the code, `user` becomes a self-descriptive string or a money
Methods `toString` and `valueOf` come from ancient times. They are not symbols (symbols did not exist that long ago), but rather "regular" string-named methods. They provide an alternative "old-style" way to implement the conversion.
If there's no `Symbol.toPrimitive` then Javascript tries to find them and try in the order:
If there's no `Symbol.toPrimitive` then JavaScript tries to find them and try in the order:
- `toString -> valueOf` for "string" hint.
- `valueOf -> toString` otherwise.

View file

@ -210,7 +210,7 @@ john = {
We can use constructor functions to make multiple similar objects. But the topic is much deeper than described here. So we'll return it later and cover more in-depth.
As of now, it's important to understand what `new` is, because Javascript provides constructor functions for many built-in language objects: like `Date` for dates, `Set` for sets and others that we plan to study.
As of now, it's important to understand what `new` is, because JavaScript provides constructor functions for many built-in language objects: like `Date` for dates, `Set` for sets and others that we plan to study.