I am trying to use the factory function way of implementing an object, and tried it with a linked list example, where I initialize an empty linked list then try to add nodes, in the add method method I set the head if it is not set, otherwise add the node to the last node in the chain. The problem is that the head never appears to get set, any ideas why?
"use strict"
const niceLog = s => {
console.log(JSON.stringify(s, null, 2))
}
function linkedList() {
let head
const node = data => {
return {
data,
next: null
}
}
const add = data => {
if (!head) {
head = node(data)
} else {
const end = node(data)
let n = head
while (n.next) n = n.next
n.next = end
}
}
const del = data => {
let n = head
if (n.data === data) head = head.next
while (n.next) {
if (n.next.data === data) {
n.next = n.next.next
return
}
n = n.next
}
}
return {
head,
add,
del
}
}
const ll = linkedList()
ll.add('cheese')
ll.add('crackers')
ll.add('tea')
ll.add('coffee')
niceLog(ll) // {}
Here is the equivalent code in es6 class syntax which does work, (I had heard that factory functions are better as they avoid the issues of new and this keywords not being set properly so thats why I was trying to use factory functions)
const niceLog = s => {
console.log(JSON.stringify(s, null, 2))
}
class Node {
constructor(data) {
this.data = data
this.next = null
}
}
class LinkedList {
constructor() {
this.head = null
}
add(data){
if (!this.head) {
this.head = new Node(data)
} else {
const end = new Node(data)
let n = this.head
while (n.next) n = n.next
n.next = end
}
}
del(data) {
let n = this.head
if (n.data === data) this.head = this.head.next
while (n.next) {
if (n.next.data === data) {
n.next = n.next.next
return
}
n = n.next
}
}
}
const ll = new LinkedList()
ll.add('cheese')
ll.add('crackers')
ll.add('tea')
ll.add('coffee')
niceLog(ll) // output =
"head": {
"data": "cheese",
"next": {
"data": "crackers",
"next": {
"data": "tea",
"next": {
"data": "coffee",
"next": null
}
}
}
}
}