Skip to content

Commit 8157323

Browse files
committed
tweak words
1 parent 1f2360b commit 8157323

File tree

1 file changed

+10
-22
lines changed

1 file changed

+10
-22
lines changed

chapters/ch05.asciidoc

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const {
9999
} = response
100100
----
101101

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."
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 it straight is to mentally replace `:` with the word "as". That way, `name: responseContactName` would read as "name as responseContactName".
103103

104104
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
105105

@@ -191,16 +191,6 @@ const finalValues = doubledValues.filter(value => value > 5)
191191
// <- [6, 8, 10]
192192
----
193193

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-
204194
Let's move onto a more interesting topic: asynchronous code flows.
205195

206196
==== 5.1.4 Navigating Callbacks, Promises, and Asynchronous Functions
@@ -490,26 +480,24 @@ Having event listeners allowed a component to receive a message, perhaps process
490480

491481
==== 5.3.4 Message Passing and the Simplicity of JSON
492482

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
483+
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 it 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:
494484

495485
[source,javascript]
496486
----
497-
class Person{
498-
constructor(name, address){
499-
this.name = name;
500-
this.address = address;
487+
class Person {
488+
constructor(name, address) {
489+
this.name = name
490+
this.address = address
501491
}
502-
speak(){
503-
alert(`My name is ${this.name}`);
492+
greet() {
493+
console.log(`Hi! My name is ${ this.name }.`)
504494
}
505495
}
506496
507-
let nico = new Person("Nico", "123 Main St");
497+
const rwanda = new Person('Rwanda', '123 Main St')
508498
----
509499

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.
500+
Although we can easily serialize our `rwanda` instance with `JSON.stringify(rwanda)`, 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, which might have a lot more functionality than merely a `greet` function. The receiving end might have no business deserializing this data back into the class instance it originated from, but in some cases there's merit to having an exact replica object back on the other end. For example, to reduce friction when passing messages between a website and a web worker, both sides should be dealing in the same data structure. In such scenario, simple JavaScript objects are ideal.
513501

514502
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.
515503

0 commit comments

Comments
 (0)