I have this code with a class in it:
console.log("calling linkedlist");
class NodeAcc {
constructor(NodeName) {
this._NodeName = NodeName;
this.next = null;
}
// Getter & Settter for AccName
get NodeName() {
return this._NodeName;
}
set NodeName(_NodeName) {
if (typeof _NodeName !== "string") throw new Error("AccName must be a string.");
this._NodeName = _NodeName;
}
};
class LinkedList {
constructor() {
this.head = null;
}
insertAtEnd(AccName) { // For adding new nodes
const newNode = new NodeAcc(AccName);
if (!this.head) {
this.head = newNode;
return;
}
let current = this.head;
while (current.next !== null) {
current = current.next;
}
current.next = newNode;
}
find(AccName) {
let current = this.head;
while (current !== null) {
if (current.AccName === AccName) {
return current;
}
current = current.next;
}
return null;
}
doesItExists(AccName) { // Same as "find" but returns booleans
let current = this.head;
while (current !== null) {
if (current.AccName === AccName) {
return true;
}
current = current.next;
}
return false;
}
printList() {
let current = this.head;
let i = 0;
while (current !== null) {
console.log(`[${i}] Account: ${current.AccName}, Value: ${current.Value}`);
current = current.next;
i++;
}
}
forEach(callback) { // Going to make my LinkedList iterable in a forEach command.
let current = this.head;
let index = 0;
while (current !== null) {
callback(current, index);
current = current.next;
index++;
}
}
}
I want to reuse this script in other dataviewjs code blocks, how can I do that?