How Can I Reference Class Instances That I Have Passed Into A Map As Keys Javascript
I am implementing a graph class using a Map as an adjacency list, with a simple class Vertex that I am using to represent each node in the graph: export class Vertex { construc
Solution 1:
Given your addVertex
method creates the Vertex
instance and the addEdge
method expects that very instance as a parameter, you need to make it available to the caller of these methods - by return
ing it:
…
addVertex(value) {
if (value) {
const vertex = newVertex(value);
this.adjacencyList.set(vertex, []);
return vertex;
}
// else throw new Error("no value given")?
}
…
Then you can use it like
const graph = newUndirectedGraph();
const vertex1 = graph.addVertex("A");
const vertex2 = graph.addVertex("B");
const vertex3 = graph.addVertex("B");
graph.addEdge(vertex1, vertex2);
graph.addEdge(vertex1, vertex3);
graph.addEdge(vertex2, vertex3);
Post a Comment for "How Can I Reference Class Instances That I Have Passed Into A Map As Keys Javascript"