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: 07_robot.md
+6-8Lines changed: 6 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -48,10 +48,10 @@ The array of strings isn't very easy to work with. What we're interested in is t
48
48
function buildGraph(edges) {
49
49
let graph = Object.create(null);
50
50
function addEdge(from, to) {
51
-
if (graph[from] == null) {
52
-
graph[from] = [to];
53
-
} else {
51
+
if (from in graph) {
54
52
graph[from].push(to);
53
+
} else {
54
+
graph[from] = [to];
55
55
}
56
56
}
57
57
for (let [from, to] of edges.map(r => r.split("-"))) {
@@ -84,9 +84,7 @@ To be able to simulate this process, we must define a virtual world that can des
84
84
85
85
If you're thinking in terms of ((object-oriented programming)), your first impulse might be to start defining objects for the various elements in the world: a ((class)) for the robot, one for a parcel, maybe one for places. These could then hold properties that describe their current ((state)), such as the pile of parcels at a location, which we could change when updating the world.
86
86
87
-
This is wrong.
88
-
89
-
At least, it usually is. The fact that something sounds like an object does not automatically mean that it should be an object in your program. Reflexively writing classes for every concept in your application tends to leave you with a collection of interconnected objects that each have their own internal, changing state. Such programs are often hard to understand and thus easy to break.
87
+
This is wrong. At least, it usually is. The fact that something sounds like an object does not automatically mean that it should be an object in your program. Reflexively writing classes for every concept in your application tends to leave you with a collection of interconnected objects that each have their own internal, changing state. Such programs are often hard to understand and thus easy to break.
90
88
91
89
{{index [state, in objects]}}
92
90
@@ -490,9 +488,9 @@ When a value is added to the group, you can create a new group with a copy of th
490
488
491
489
The class's ((constructor)) can take such an array as argument and store it as the instance's (only) property. This array is never updated.
492
490
493
-
{{index "static method"}}
491
+
{{index "static property"}}
494
492
495
-
To add a property (`empty`) to a constructor that is not a method, you have to add it to the constructor after the class definition, as a regular property.
493
+
To add the `empty` property to the constructor, you can declare it as a static property.
496
494
497
495
You need only one `empty` instance because all empty groups are the same and instances of the class don't change. You can create many different groups from that single empty group without affecting it.
0 commit comments