You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md
+8-13Lines changed: 8 additions & 13 deletions
Original file line number
Diff line number
Diff line change
@@ -6,26 +6,21 @@ let str = "Hello";
6
6
7
7
str.test=5; // (*)
8
8
9
-
alert(str.test);
9
+
alert(str.test);
10
10
```
11
11
12
-
There may be two kinds of result:
13
-
1.`undefined`
14
-
2. An error.
12
+
Depending on whether you have `use strict` or not, the result may be:
13
+
1.`undefined` (no string)
14
+
2. An error (strict mode).
15
15
16
16
Why? Let's replay what's happening at line `(*)`:
17
17
18
18
1. When a property of `str` is accessed, a "wrapper object" is created.
19
-
2.The operation with the property is carried out on it. So, the object gets the `test` property.
20
-
3.The operation finishes and the "wrapper object" disappears.
19
+
2.In strict mode, writing into it is an error.
20
+
3.Otherwise, the operation with the property is carried on, the object gets the `test` property, but after that the "wrapper object" disappears.
21
21
22
-
So, on the last line, `str` has no trace of the property. A new wrapper object for every object operation on a string.
23
-
24
-
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.
22
+
So, without strict mode, in the last line `str` has no trace of the property.
25
23
26
24
**This example clearly shows that primitives are not objects.**
27
25
28
-
They just can not store data.
29
-
30
-
All property/method operations are performed with the help of temporary objects.
Copy file name to clipboardExpand all lines: 1-js/05-data-types/01-primitives-methods/article.md
+6-6Lines changed: 6 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,7 @@ A primitive
14
14
An object
15
15
16
16
- Is capable of storing multiple values as properties.
17
-
- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript; functions, for example, are objects.
17
+
- Can be created with `{}`, for instance: `{name: "John", age: 30}`. There are other kinds of objects in JavaScript: functions, for example, are objects.
18
18
19
19
One of the best things about objects is that we can store a function as one of its properties.
20
20
@@ -48,7 +48,7 @@ The solution looks a little bit awkward, but here it is:
48
48
49
49
1. Primitives are still primitive. A single value, as desired.
50
50
2. The language allows access to methods and properties of strings, numbers, booleans and symbols.
51
-
3.When this happens, a special "object wrapper" that provides the extra functionality is created, and then is destroyed.
51
+
3.In order for that to work, a special "object wrapper" that provides the extra functionality is created, and then is destroyed.
52
52
53
53
The "object wrappers" are different for each primitive type and are called: `String`, `Number`, `Boolean` and `Symbol`. Thus, they provide different sets of methods.
54
54
@@ -91,18 +91,18 @@ In JavaScript, that's also possible for historical reasons, but highly **unrecom
91
91
For instance:
92
92
93
93
```js run
94
-
alert( typeof1 ); // "number"
94
+
alert( typeof0 ); // "number"
95
95
96
-
alert( typeofnewNumber(1) ); // "object"!
96
+
alert( typeofnewNumber(0) ); // "object"!
97
97
```
98
98
99
-
And because what follows, `zero`, is an object, the alert will show up:
99
+
Objects are always truthy in `if`, so here the alert will show up:
100
100
101
101
```js run
102
102
let zero =newNumber(0);
103
103
104
104
if (zero) { // zero is true, because it's an object
0 commit comments