work
This commit is contained in:
parent
4c531b5ae7
commit
d4c714cbe1
261 changed files with 7370 additions and 546 deletions
|
@ -1,31 +0,0 @@
|
|||
|
||||
Try running it:
|
||||
|
||||
```js run
|
||||
let str = "Hello";
|
||||
|
||||
str.test = 5; // (*)
|
||||
|
||||
alert(str.test);
|
||||
```
|
||||
|
||||
There may be two kinds of result:
|
||||
1. `undefined`
|
||||
2. An error.
|
||||
|
||||
Why? Let's replay what's happening at line `(*)`:
|
||||
|
||||
1. When a property of `str` is accessed, a "wrapper object" is created.
|
||||
2. The operation with the property is carried out on it. So, the object gets the `test` property.
|
||||
3. The operation finishes and the "wrapper object" disappears.
|
||||
|
||||
So, on the last line, `str` has no trace of the property. A new wrapper object for every object operation on a string.
|
||||
|
||||
Some browsers though may decide to further limit the programmer and disallow to assign properties to primitives at all. That's why in practice we can also see errors at line `(*)`. It's a little bit farther from the specification though.
|
||||
|
||||
**This example clearly shows that primitives are not objects.**
|
||||
|
||||
They just can not store data.
|
||||
|
||||
All property/method operations are performed with the help of temporary objects.
|
||||
|
|
@ -1,18 +0,0 @@
|
|||
importance: 5
|
||||
|
||||
---
|
||||
|
||||
# Can I add a string property?
|
||||
|
||||
|
||||
Consider the following code:
|
||||
|
||||
```js
|
||||
let str = "Hello";
|
||||
|
||||
str.test = 5;
|
||||
|
||||
alert(str.test);
|
||||
```
|
||||
|
||||
How do you think, will it work? What will be shown?
|
|
@ -214,7 +214,6 @@ In this case `this` is `undefined` in strict mode. If we try to access `this.nam
|
|||
|
||||
In non-strict mode (if you forgot `use strict`) the value of `this` in such case will be the *global object* (`"window"` for browser, we'll study it later). This is just a historical thing that `"use strict"` fixes.
|
||||
|
||||
|
||||
Please note that usually a call of a function using `this` without an object is not normal, but rather a programming mistake. If a function has `this`, then it is usually meant to be called in the context of an object.
|
||||
|
||||
```smart header="The consequences of unbound `this`"
|
||||
|
@ -224,7 +223,6 @@ The idea of unbound, run-time evaluated `this` has both pluses and minuses. From
|
|||
|
||||
Here we are not to judge whether this language design decision is good or bad. We will understand how to work with it, how to get benefits and evade problems.
|
||||
```
|
||||
|
||||
## Internals: Reference Type
|
||||
|
||||
An intricate method call can loose `this`, for instance:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue