Skip to content

Commit 2281311

Browse files
authored
Merge pull request mjavascript#6 from arackaf/feature/chapter5-feedback
Chapter 5 tweaks
2 parents 4bd09b1 + 1c222a8 commit 2281311

File tree

1 file changed

+76
-2
lines changed

1 file changed

+76
-2
lines changed

chapters/ch05.asciidoc

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,52 @@ const {
7777
} = response
7878
----
7979

80-
One contention point might be that, at times, the deeply destructured property name doesn't make sense outside of its context. For instance, we've introduce `name` to our scope, but it's the name of the contact for the listing, not to be confused with the name of the listing itself. In these cases, the alternative may be to give `response.contact.name` an alias where we add the parent property name as a prefix, so that we're left with a binding named `contactName` or `responseContactName`.
80+
At times, a deeply destructured property name may not make sense outside of its context. For instance, we introduce `name` to our scope, but it's the name of the contact for the listing, not to be confused with the name of the listing itself. We can clarify this by giving the contact's `name` an alias, like `contactName` or `responseContactName`.
81+
82+
[source,javascript]
83+
----
84+
const {
85+
title,
86+
description,
87+
askingPrice,
88+
features: {
89+
area,
90+
bathrooms,
91+
bedrooms,
92+
amenities
93+
},
94+
contact: {
95+
name: responseContactName,
96+
phone,
97+
email
98+
}
99+
} = response
100+
----
101+
102+
When using `:` to alias, it can be difficult at first to remember whether the original name, or the aliased name comes first. One helpful way to keep this straight is to mentally replace `:` with the word "as." So `name: responseContactName` would read as "name as responseContactName."
103+
104+
We can even have the same property listed twice, if we wanted to destructure some of its contents, while also maintaining access to the object itself. For example, if we wanted to destructure the `contact` object's contents, like we do above, but also take a reference to the whole `contact` object, we can do the following
105+
106+
[source,javascript]
107+
----
108+
const {
109+
title,
110+
description,
111+
askingPrice,
112+
features: {
113+
area,
114+
bathrooms,
115+
bedrooms,
116+
amenities
117+
},
118+
contact: responseContact,
119+
contact: {
120+
name: responseContactName,
121+
phone,
122+
email
123+
}
124+
} = response
125+
----
81126

82127
Object spread helps us create a shallow copy of an object using a little native syntax. We can also combine object spread with our own properties, so that we create a copy that also overwrites the values in the original object we're spreading.
83128

@@ -146,6 +191,16 @@ const finalValues = doubledValues.filter(value => value > 5)
146191
// <- [6, 8, 10]
147192
----
148193

194+
NOTE - this doesn't make sense. The original example assigned to the SAME variable over and over again. Wouldn't the simplified, equivalent code just chain the subsequent calls?
195+
196+
[source,javascript]
197+
----
198+
let values = [1, 2, 3, 4, 5]
199+
.map(value => value * 2)
200+
.filter(value => value > 5)
201+
// <- [6, 8, 10]
202+
----
203+
149204
Let's move onto a more interesting topic: asynchronous code flows.
150205

151206
==== 5.1.4 Navigating Callbacks, Promises, and Asynchronous Functions
@@ -435,7 +490,26 @@ Having event listeners allowed a component to receive a message, perhaps process
435490

436491
==== 5.3.4 Message Passing and the Simplicity of JSON
437492

438-
When it comes to ServiceWorker, web workers, browser extensions, frames, API calls, or WebSocket integrations, we might run into issues if we don't plan for robust data serialization ahead of time. This is generally when using classes to represent data starts to breaks down, because we'd need a way to serialize instances into their raw data counterparts (typically JSON) before sending it over the wire, since the receiving end wouldn't know how to decode a JavaScript class instance, and there isn't a standardized way of encoding them either. At the same time, we'd need a way to deserialize this plain JSON data on the receiving end, creating another class instance making sure it has the correct state.
493+
When it comes to ServiceWorker, web workers, browser extensions, frames, API calls, or WebSocket integrations, we might run into issues if we don't plan for robust data serialization ahead of time. This is a place where using classes to represent data can break down, because we need a way to serialize class instances into raw data (typically JSON) before sending over the wire, and, crucially, the recipient needs to decode this JSON back into a class instance. It's the second part where classes start to fail, since there isn't a standardized way of reconstructing a class instance from JSON. For example
494+
495+
[source,javascript]
496+
----
497+
class Person{
498+
constructor(name, address){
499+
this.name = name;
500+
this.address = address;
501+
}
502+
speak(){
503+
alert(`My name is ${this.name}`);
504+
}
505+
}
506+
507+
let nico = new Person("Nico", "123 Main St");
508+
----
509+
510+
Although we can easily serialize our `nico` instance with `JSON.stringify(nico);`, and then send it over the wire, the code on the other end will have no standard way of turning this JSON back into an instance of our `Person` class.
511+
512+
NOTE: then again, can you clarify why the receiving end would need to deserialize the data back into class instances, rather than just use raw object literals? Assymetric representations of the same data is likely uglfy, and needlessly complex, and so possibly the only reason, and a good one, but it might be worth spelling out explicitly.
439513

440514
JSON, a subset of the JavaScript grammar, was purpose-built for this use case, where we often have to serialize data, send it over the wire, and deserialize it on the other end. Plain JavaScript objects are a great way to store data in our applications, offer frictionless serialization out the box, and lead to cleaner data structures because we can keep logic decoupled from the data.
441515

0 commit comments

Comments
 (0)