Javascript 在 class 构造函数中调用 class (ES6)
Javascript calling class within class constructor (ES6)
我正在尝试在 javascript 中实现一个简单的堆栈(我知道它可以用一个简单的数组来实现,但这不是我的意思。)
所以这是我的设计:
pop(): remove and return the item from the top of the stack
push(item): add the item at the top of the stack
peek(): return the item from the top of the stack
isEmpty(): return true if stack is empty
它将有一个 top
属性 来跟踪顶部元素,这是我的 class 定义:
class Stack {
constructor(data) {
this.data = data;
this.next = null;
this.top = Stack(data); // <- Here's my problem
}
pop() {
if(this.top == null) return new Error('Trying to pop, but the stack is empty.');
let item = this.top.data;
this.top = this.top.next;
return item;
}
push(data) {
let item = new Stack(data);
item.next = this.top;
this.top = item;
}
peek() {
if(this.top == null) return new Error('Peeking the stack, but the stack is empty.');
return this.top.data;
}
isEmpty() {
return top == null;
}
}
我希望 top
属性 成为 Stack
元素,但是,如您所见,这会使我陷入无限循环。我 可以 将 top
设置为具有 data, next, and top
的对象。但这是唯一的方法吗?也许我可以有一个成员函数在初始化 class 时生成 top
属性?但是,我最终还是要将它设置为一个对象而不是 Stack
对象。
有什么建议吗?
您混淆了堆栈和数据。您的代码试图做的是将整个堆栈推入构造函数中的堆栈,可能 运行 进入无限递归。
定义什么是data
,也许把它变成一个class数据,或者把它当作一个数组。在您的 Stack 构造函数中,只需将数据(作为原始数据或作为 class 数据的实例)推入堆栈。
如果您想将堆栈实现为链表,则该列表需要在某个时刻结束,其 "next"/"top" 引用为 null
。
但是,您不应将堆栈(代表整个事物,可能为空)与堆栈元素(堆栈中的一个数据项)混淆。不要把它们混成同一个class,用两个分开的classes:
class StackElement {
constructor(data) { // takes the data as an argument (possibly also `next`)
this.data = data;
this.next = null;
}
}
class Stack {
constructor() { // takes no arguments
this.top = null; // initialises reference with nothing
// has no other properties
}
…
}
我将这些方法的实现作为练习留给 reader。
我正在尝试在 javascript 中实现一个简单的堆栈(我知道它可以用一个简单的数组来实现,但这不是我的意思。) 所以这是我的设计:
pop(): remove and return the item from the top of the stack
push(item): add the item at the top of the stack
peek(): return the item from the top of the stack
isEmpty(): return true if stack is empty
它将有一个 top
属性 来跟踪顶部元素,这是我的 class 定义:
class Stack {
constructor(data) {
this.data = data;
this.next = null;
this.top = Stack(data); // <- Here's my problem
}
pop() {
if(this.top == null) return new Error('Trying to pop, but the stack is empty.');
let item = this.top.data;
this.top = this.top.next;
return item;
}
push(data) {
let item = new Stack(data);
item.next = this.top;
this.top = item;
}
peek() {
if(this.top == null) return new Error('Peeking the stack, but the stack is empty.');
return this.top.data;
}
isEmpty() {
return top == null;
}
}
我希望 top
属性 成为 Stack
元素,但是,如您所见,这会使我陷入无限循环。我 可以 将 top
设置为具有 data, next, and top
的对象。但这是唯一的方法吗?也许我可以有一个成员函数在初始化 class 时生成 top
属性?但是,我最终还是要将它设置为一个对象而不是 Stack
对象。
有什么建议吗?
您混淆了堆栈和数据。您的代码试图做的是将整个堆栈推入构造函数中的堆栈,可能 运行 进入无限递归。
定义什么是data
,也许把它变成一个class数据,或者把它当作一个数组。在您的 Stack 构造函数中,只需将数据(作为原始数据或作为 class 数据的实例)推入堆栈。
如果您想将堆栈实现为链表,则该列表需要在某个时刻结束,其 "next"/"top" 引用为 null
。
但是,您不应将堆栈(代表整个事物,可能为空)与堆栈元素(堆栈中的一个数据项)混淆。不要把它们混成同一个class,用两个分开的classes:
class StackElement {
constructor(data) { // takes the data as an argument (possibly also `next`)
this.data = data;
this.next = null;
}
}
class Stack {
constructor() { // takes no arguments
this.top = null; // initialises reference with nothing
// has no other properties
}
…
}
我将这些方法的实现作为练习留给 reader。