【发布时间】:2017-03-30 18:11:55
【问题描述】:
我正在尝试了解范围如何与节点模块一起使用。我有一个 IIFE 来初始化一个 LinkedList,它为每个元素使用 Node 对象。
使用 IIFE 来避免污染全局范围
IIFE 之后,Node 去哪儿了?有什么方法可以访问需要对象中的节点,即测试类?
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?
【问题讨论】:
-
你想做什么?
标签: javascript node.js module