Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Javascript中实现堆栈类时遇到问题_Javascript_Algorithm_Data Structures_Stack - Fatal编程技术网

在Javascript中实现堆栈类时遇到问题

在Javascript中实现堆栈类时遇到问题,javascript,algorithm,data-structures,stack,Javascript,Algorithm,Data Structures,Stack,我正在创建我的堆栈类。我阅读了一本javascript数据结构书,但我更改了一些函数,我不断收到一个错误,上面写着“s.length不是函数”。我有一个length函数,但我想知道,既然javascript中有一个关键字'length',那么与函数同名可能会引起问题 // LIFO function Stack() { this.dataStore = []; // top of the stack this.top = 0; this.push = push

我正在创建我的堆栈类。我阅读了一本javascript数据结构书,但我更改了一些函数,我不断收到一个错误,上面写着“s.length不是函数”。我有一个length函数,但我想知道,既然javascript中有一个关键字'length',那么与函数同名可能会引起问题

// LIFO

function Stack() 
{
    this.dataStore = [];
    // top of the stack
    this.top = 0;
    this.push = push;
    this.pop = pop;
    this.peek = peek;
 }

function push(element)
{
    // when new element is pushed, it needs to be stored
    // in the top position and top var needs to be incremented
    // so the new top is the next empty pos in the array 
    //this.dataStore(this.top++) = element;
    // the increment op after the call ensures that the 
    // current value of top is used to place the new element
    // at the top of the stack before top is incremented 
    this.dataStore.push(element);
 }

function pop()
{
    // returns element in top pos of stack and then decrements
    // the top variable
    //return this.dataStore[--this.top];
    return this.dataStore.pop(element);
}

function peek()
{
    // returns the top element of the stack by accessing 
    // the element at the top-1 position of the array
    //return this.dataStore[this.top-1];
    return this.dataStore[items.length-1];
}

function length()
{
    //return this.top;
    return this.dataStore.length;
}

function clear()
{
    //return this.top = 0;
    return this.dataStore = [];
}

var s = new Stack();
s.push("David");
s.push("Raymond");
s.push("Bryan"); 
console.log("length: " + s.length());
像这样的“不是函数”错误通常意味着您试图将非函数对象属性作为函数调用。这通常是通过在属性后面加括号来完成的

您在这里执行了以下操作:
console.log(“长度:+s.length())

删除括号:

console.log(“长度:+s.length”)

但是堆栈对象没有长度属性,因此将用一个错误替换另一个错误。我不太确定你想通过检查物体的长度来达到什么目的,但是请检查这个问题,看看如何实现

这超出了问题的范围,但您遇到了问题,因为您正试图将
推送到对象上,而您可能希望推送到对象内的
数据存储
数组上,如下所示:

s.dataStore.push("David");
像这样的“不是函数”错误通常意味着您试图将非函数对象属性作为函数调用。这通常是通过在属性后面加括号来完成的

您在这里执行了以下操作:
console.log(“长度:+s.length())

删除括号:

console.log(“长度:+s.length”)

但是堆栈对象没有长度属性,因此将用一个错误替换另一个错误。我不太确定你想通过检查物体的长度来达到什么目的,但是请检查这个问题,看看如何实现

这超出了问题的范围,但您遇到了问题,因为您正试图将
推送到对象上,而您可能希望推送到对象内的
数据存储
数组上,如下所示:

s.dataStore.push("David");

首先,我不认为使用这种模式是个好主意:

function MyClass () {
  this.method = method;
}
function method () {
  // ...
}
它污染了名称空间,
length
作为一个公共属性,它很快就会变得混乱。我更喜欢在定义构造函数后使用prototype对象的显式重写,这避免了全局函数作为方法的需要

也许这样更好?(为简洁起见,省略注释)

那么你的例子就行了

length
视为用户定义的属性
通过快速测试,javascript可以用这种方式覆盖
length
。这是因为
length
不是对象的属性(但是数组是的),所以您可以在自己的类中将其用作属性或方法名。

首先,我认为使用此模式不是一个好主意:

function MyClass () {
  this.method = method;
}
function method () {
  // ...
}
它污染了名称空间,
length
作为一个公共属性,它很快就会变得混乱。我更喜欢在定义构造函数后使用prototype对象的显式重写,这避免了全局函数作为方法的需要

也许这样更好?(为简洁起见,省略注释)

那么你的例子就行了

length
视为用户定义的属性
通过快速测试,javascript可以用这种方式覆盖
length
。这是因为
length
不是对象的属性(但是数组是的),所以您可以在自己的类中将其作为属性或方法名使用。

无论您阅读的是什么数据结构书,它都是错误的;至少在JS中是这样

在JS中,您需要在实例化对象的原型中登记实用程序方法,以便它们不会为每个实例化的堆栈对象占用冗余内存空间

到目前为止,我的观点是一致的,但是这个堆栈是使用数组子分类的完美选择,这样您就不需要在
堆栈()
构造函数的原型下重新实现标准数组方法,如
.pop()
.push()
。相反,您可以使
Array.prototype
成为
Stack.prototype
的超类(此处为子类)。最重要的是,由于我们的堆栈对象将从一个适当的数组对象继承,所以它们的神奇长度属性将像任何数组一样工作。(即使您通过索引分配项目。)

让我们看看实现

函数堆栈(…a){
var stack=新数组(…a);
setPrototypeOf(stack,stack.prototype);
返回栈;
}
Stack.prototype=Object.create(Array.prototype);//现在,堆栈可以完全访问数组方法。
Stack.prototype.constructor=Stack;//现在堆栈是一个合适的构造函数
Stack.prototype.peak=function(){返回此[this.length-1]};//将Stack“only”方法添加到Stack.prototype。
var s=新堆栈(1,2,3,4,1);
console.log(s.peak());
s[s.长度]=7;
控制台。对数(s.长度);
s、 推(42);
控制台日志;

控制台。对数(s.长度)无论你在读什么数据结构书,它都是做错了;至少在JS中是这样

在JS中,您需要在实例化对象的原型中登记实用程序方法,以便它们不会为每个实例化的堆栈对象占用冗余内存空间

到目前为止,我的观点是一致的,但是这个堆栈是使用数组子分类的完美选择,这样您就不需要在
堆栈()
构造函数的原型下重新实现标准数组方法,如
.pop()
.push()
。相反,您可以使
Array.prototype
成为
Stack.prototype
的超类(此处为子类)。最重要的是,由于我们的堆栈对象将从一个适当的数组对象继承,所以它们的神奇长度属性将像任何数组一样工作。(即使您通过索引分配项目。)

让我们看看