Select Page

I’ve started the Codecademy Pass the Technical Interview with JavaScript path.

The first section is on nodes. In JavaScript, a node is a class that has data and a link to another node. 

What is a class in JavaScript? 

Classes are bits of code that encompass multiple objects, methods and allow manipulation for its member variables and functions. Within each language, a class has different syntax and the same holds true for Javascript. In this language, a class is simply a variant of functions. Says https://www.educative.io/answers/what-are-classes-in-javascript

Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on prototypes but also have some syntax and semantics that are not shared with ES5 class-like semantics. Says https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes

What is an object in JavaScript?

An object is a collection of properties, and a property is an association between a name (or key) and a value. A property’s value can be a function, in which case the property is known as a method. In addition to objects that are predefined in the browser, you can define your own objects. Says https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

They also say 

A JavaScript object has properties associated with it. A property of an object can be explained as a variable that is attached to the object. Object properties are basically the same as ordinary JavaScript variables, except for the attachment to objects. The properties of an object define the characteristics of the object. You access the properties of an object with a simple dot-notation:

objectName.propertyName

This is the first node we defined in the Codecademy class

class Node {
constructor(data) {
this.data = data;
this.next = null;
}
}
const firstNode = new Node(‘I am an instance of a Node!’);

We declare the class with “class”

But what is a constructor?

The constructor method is a special method of a class for creating and initializing an object instance of that class
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/constructor

To create an initialize an object instance of a class, we use the keyword “new”, as in const firstNode=newNode(‘I am an instance of a Node!’)

Our constructor will accept a parameter that we’ve named “data” and it it has two properties: data and next.

the this. notation is necessary because we are referring to properties inside of the class.

After creating the Node class and giving it a constructor method, we need a way to update the “next” property, so we can add links to the nodes.

So we create a new method called setNextNode that takes a node parameter and uses it to set the “next” property to the node we passed into the parameter
class Node {
constructor(data) {
this.data = data;
this.next = null;
}
setNextNode(node) {
this.next = node;
}
}

We can then create a variable secondNode and make firstNode link to secondNode with

firstNode.setNextNode(secondNode);

Now look at the properties of the firstNode object:

node {
data: 'I am an instance of a Node!',
next: Node { data: 'I am another instance of a node.', next: null } }

But what if some fool passed something that was not an instance of the Node class into the setNextNode parameter? Does it make any sense for a node to link, for example, to the number 5? No. All Node objects have to link to other Node objects. So we add the following code:


setNextNode(node) {
if (node instanceof Node || node === null) {
this.next = node;
} else {
throw new error("node must be an instance of the Node class")
}
}

The if statement checks to make sure that the passed-in node is either an instance of the Node class or equal to null. If so, we update the “next” property; if now, we throw an error and grind the operation to a halt.

We added a method inside the Node class to step from an object to the object it links to:


getNextNode() {
return this.next;
}

To walk through a series of linked nodes:


while (currentNode !== null) {
console.log(currentNode.data);
currentNode = currentNode.getNextNode();
}