From 2f0c37db83e4734ab098dbdafb31fd72c900cc1e Mon Sep 17 00:00:00 2001 From: Ilya Kantor Date: Sun, 22 Nov 2020 14:29:25 +0300 Subject: [PATCH] fixes --- 1-js/04-object-basics/01-object/article.md | 24 ------------------ .../02-object-copy/article.md | 25 ++++++++++++++++++- .../3-events-change-input/article.md | 6 +++-- 3 files changed, 28 insertions(+), 27 deletions(-) diff --git a/1-js/04-object-basics/01-object/article.md b/1-js/04-object-basics/01-object/article.md index 513f2f3e..ed8a3f4d 100644 --- a/1-js/04-object-basics/01-object/article.md +++ b/1-js/04-object-basics/01-object/article.md @@ -92,30 +92,6 @@ let user = { ``` That is called a "trailing" or "hanging" comma. Makes it easier to add/remove/move around properties, because all lines become alike. -````smart header="Object with const can be changed" -Please note: an object declared as `const` *can* be modified. - -For instance: - -```js run -const user = { - name: "John" -}; - -*!* -user.name = "Pete"; // (*) -*/!* - -alert(user.name); // Pete -``` - -It might seem that the line `(*)` would cause an error, but no. The `const` fixes the value of `user`, but not its contents. - -The `const` would give an error only if we try to set `user=...` as a whole. - -There's another way to make constant object properties, we'll cover it later in the chapter . -```` - ## Square brackets For multiword properties, the dot access doesn't work: diff --git a/1-js/04-object-basics/02-object-copy/article.md b/1-js/04-object-basics/02-object-copy/article.md index 1e2ed1aa..bbaff9ca 100644 --- a/1-js/04-object-basics/02-object-copy/article.md +++ b/1-js/04-object-basics/02-object-copy/article.md @@ -73,7 +73,6 @@ admin.name = 'Pete'; // changed by the "admin" reference alert(*!*user.name*/!*); // 'Pete', changes are seen from the "user" reference ``` - It's just as if we had a cabinet with two keys and used one of them (`admin`) to get into it. Then, if we later use another key (`user`) we can see changes. ## Comparison by reference @@ -230,6 +229,30 @@ To fix that, we should use the cloning loop that examines each value of `user[ke We can use recursion to implement it. Or, not to reinvent the wheel, take an existing implementation, for instance [_.cloneDeep(obj)](https://lodash.com/docs#cloneDeep) from the JavaScript library [lodash](https://lodash.com). +```smart header="Const objects can be modified" +An important "side effect" of storing objects as references is that an object declared as `const` *can* be modified. + +For instance: + +```js run +const user = { + name: "John" +}; + +*!* +user.name = "Pete"; // (*) +*/!* + +alert(user.name); // Pete +``` + +It might seem that the line `(*)` would cause an error, but no. The value of `user` is constant, it must always reference the same object. But properties of that object are free to change. + +In other words, the `const user` gives an error only if we try to set `user=...` as a whole, and that's all. + +That said, if we really need to make constant object properties, it's also possible, but using totally different methods, we'll mention that in the chapter . +``` + ## Summary Objects are assigned and copied by reference. In other words, a variable stores not the "object value", but a "reference" (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object. diff --git a/2-ui/4-forms-controls/3-events-change-input/article.md b/2-ui/4-forms-controls/3-events-change-input/article.md index b43a305b..1355e8bb 100644 --- a/2-ui/4-forms-controls/3-events-change-input/article.md +++ b/2-ui/4-forms-controls/3-events-change-input/article.md @@ -76,11 +76,13 @@ For instance, the code below prevents all such events and shows what we are tryi Please note, that it's possible to copy/paste not just text, but everything. For instance, we can copy a file in the OS file manager, and paste it. -There's a list of methods [in the specification](https://www.w3.org/TR/clipboard-apis/#dfn-datatransfer) that can work with different data types including files, read/write to the clipboard. +That's because `clipboardData` implements `DataTransfer` interface, commonly used for drag'n'drop and copy/pasting. It's bit beyound our scope now, but you can find its methods [in the specification](https://html.spec.whatwg.org/multipage/dnd.html#the-datatransfer-interface). -But please note that clipboard is a "global" OS-level thing. Most browsers allow read/write access to the clipboard only in the scope of certain user actions for the safety, e.g. in `onclick` event handlers. +```warn header="ClipboardAPI: user safety restrictions" +The clipboard is a "global" OS-level thing. So most browsers allow read/write access to the clipboard only in the scope of certain user actions for the safety, e.g. in `onclick` event handlers. Also it's forbidden to generate "custom" clipboard events with `dispatchEvent` in all browsers except Firefox. +``` ## Summary