TypeError:无法读取属性'元素' null

时间:2018-01-26 02:00:03

标签: javascript

它发生错误,如TypeError: Cannot read property 'element' of null enter image description here

我用这样的JavaScript编写LinkedList代码,我定义了两个方法insertfind,但这在find TypeError: Cannot read property 'element' of null中出现错误

function Node(element) {
    this.element = element;
    this.next = null;
}

function LList() {
    this.head = new Node("head");
    this.find = find;
    this.insert = insert;
}

function insert(newElement, item) {
    var newNode = new Node(newElement);
    var current = this.find(item);
    newNode.next = current.next;
    current.next = newNode;
}

function find(item) {
    let currNode = this.head;
    while (currNode.element != item) {
        currNode = currNode.next;
    }
    return currNode;
}

var cities = new LList();
cities.insert("Conway", "head");
cities.insert("Rust", "Conway");
cities.find("Rust");
cities.display();

我看到currNodenull,但我确实给了currNode值。

0 个答案:

没有答案