This commit is contained in:
Ilya Kantor 2019-05-04 22:34:27 +02:00
parent 05f45021ca
commit 1ba420f511
2 changed files with 14 additions and 19 deletions

View file

@ -9,23 +9,18 @@ str.test = 5; // (*)
alert(str.test);
```
There may be two kinds of result:
1. `undefined`
2. An error.
Depending on whether you have `use strict` or not, the result may be:
1. `undefined` (no string)
2. An error (strict mode).
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.
2. In strict mode, writing into it is an error.
3. Otherwise, the operation with the property is carried on, the object gets the `test` property, but after that 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.
So, without strict mode, in the last line `str` has no trace of the property.
**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.
They can't store additional data.

View file

@ -14,7 +14,7 @@ A primitive
An object
- Is capable of storing multiple values as properties.
- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript; functions, for example, are objects.
- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript: functions, for example, are objects.
One of the best things about objects is that we can store a function as one of its properties.
@ -48,7 +48,7 @@ The solution looks a little bit awkward, but here it is:
1. Primitives are still primitive. A single value, as desired.
2. The language allows access to methods and properties of strings, numbers, booleans and symbols.
3. When this happens, a special "object wrapper" that provides the extra functionality is created, and then is destroyed.
3. In order for that to work, a special "object wrapper" that provides the extra functionality is created, and then is destroyed.
The "object wrappers" are different for each primitive type and are called: `String`, `Number`, `Boolean` and `Symbol`. Thus, they provide different sets of methods.
@ -91,18 +91,18 @@ In JavaScript, that's also possible for historical reasons, but highly **unrecom
For instance:
```js run
alert( typeof 1 ); // "number"
alert( typeof 0 ); // "number"
alert( typeof new Number(1) ); // "object"!
alert( typeof new Number(0) ); // "object"!
```
And because what follows, `zero`, is an object, the alert will show up:
Objects are always truthy in `if`, so here the alert will show up:
```js run
let zero = new Number(0);
if (zero) { // zero is true, because it's an object
alert( "zero is truthy?!?" );
alert( "zero is truthy!?!" );
}
```