From 1ba420f511df75e335ae324713dbb9f5aba039ec Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sat, 4 May 2019 22:34:27 +0200 Subject: [PATCH] fixes --- .../1-string-new-property/solution.md | 21 +++++++------------ .../01-primitives-methods/article.md | 12 +++++------ 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md index a169f776..4db6bb72 100644 --- a/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md +++ b/1-js/05-data-types/01-primitives-methods/1-string-new-property/solution.md @@ -6,26 +6,21 @@ let str = "Hello"; str.test = 5; // (*) -alert(str.test); +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. diff --git a/1-js/05-data-types/01-primitives-methods/article.md b/1-js/05-data-types/01-primitives-methods/article.md index a2dcceb1..bc94ef99 100644 --- a/1-js/05-data-types/01-primitives-methods/article.md +++ b/1-js/05-data-types/01-primitives-methods/article.md @@ -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!?!" ); } ```