JS 로의 길을 배우고 있지만 (프로그래밍에 처음이 아닙니다). 그래서 JS로 놀기 위해 LinkedList를 구현하려고합니다.변수 항상 NaN
count
이 항상 NaN
을 반환하는 것을 제외하고는 정상적으로 작동합니다. 내가 봤 거든, 그 이유는 내가 처음에 숫자로 count
을 설정 아니었다 고 생각했지만, 그랬어.
function LinkedList() {
var head = null,
tail = null,
count = 0;
var insert = function add(data)
{
// Create the new node
var node = {
data: data,
next: null
};
// Check if list is empty
if(this.head == null)
{
this.head = node;
this.tail = node;
node.next = null;
}
// If node is not empty
else
{
var current = this.tail;
current.next = node;
this.tail = node;
node.next = null;
}
this.count++;
};
return {
Add: insert,
};
}
var list = new LinkedList();
list.Add("A");
list.Add("B");
어떻게'count'가'NaN'입니까? –
당신의 카운트를 글로벌하게 만드십시오, 그러면 작동하길 바랍니다. – EnterJQ
당신은 head, tail, count라는 3 개의 private 변수를 만들었습니다. 하지만'add'에서'this'를 사용하여 public 변수로 참조하려고 시도합니다. –