I am trying to understand how scope works with node modules. I have an IIFE to initialize a LinkedList, which uses Node object for each element.
using IIFE to avoid polluting global scope
After IIFE, where does the Node go? Is there any way I can access Node in require object i.e. test class?
linkedList.js
(function LinkedListInit() {
module.exports = new LinkedList();
function LinkedList() {
this.head;
}
// LinkedList Node
function Node(data, next) {
this.data = data;
this.next = next;
}
LinkedList.prototype.insert = insert;
function insert(data) {
var curr = this.head,
newNode = new Node(data);
if (this.head == null) {
this.head = newNode;
} else {
while (curr.next != null) {
curr = curr.next;
}
curr.next = newNode;
}
return this;
}
}());
linkedListTest.js
var linkedList = require('../../js/ds/linkedList');
// Test cases for linkedList.insert(val); works fine.
// Can I access Node here?