为什么我的 push() 方法不断在我的数组中插入一个“未定义”的元素
Why does my push() method keep inserting an element of 'undefined' into my array?
为什么我的push()方法每次都不断将'undefined'的元素作为第一个元素插入到我的数组中?
var elements;
//Create a namespace.
var dataStructures = {
//Create a class called myStack which is a property of the dataStructures (object).
myStack: function () {
var topOfmyStack;
//Add the push method to the myStack object
this.push = function (element) {
//If no value was entered then stay at zero index and insert nothing
if (typeof (elements) === 'undefined') {
elements = [];
topOfmyStack = 0;
}
//Get the next empty space in the stack
topOfmyStack++;
//Place the new value in that empty space
elements[topOfmyStack] = element;
}
}
第一次,您将topOfmyStack
设置为 0;对于每个调用(包括第一次调用),您在添加到数组之前将 1 添加到topOfmyStack
。所以第一次,这变成了 1,而不是 0。
将其设置为 -1 应该可以解决此问题
var elements;
//Create a namespace.
var dataStructures = {
//Create a class called myStack which is a property of the dataStructures (object).
myStack: function () {
var topOfmyStack;
//Add the push method to the myStack object
this.push = function (element) {
//If no value was entered then stay at zero index and insert nothing
if (typeof (elements) === 'undefined') {
elements = [];
topOfmyStack = -1;
}
//Get the next empty space in the stack
topOfmyStack++;
//Place the new value in that empty space
elements[topOfmyStack] = element;
}
}
或者更好的是将元素推到数组上:
var elements;
//Create a namespace.
var dataStructures = {
//Create a class called myStack which is a property of the dataStructures (object).
myStack: function () {
//Add the push method to the myStack object
this.push = function (element) {
//If no value was entered then stay at zero index and insert nothing
if (typeof (elements) === 'undefined') {
elements = [];
}
//Place the new value in that empty space
elements.push(element);
}
}
这行代码
var topOfmyStack;
最初是undefined
你只在下面的代码中定义它
if (typeof (elements) === 'undefined') {
elements = [];
topOfmyStack = 0;
} // here topOfmyStack is still undefined
所以将代码更改为
var topOfmyStack = 0;
所以topOfmyStack++
不会出错。
//Create a namespace.
var dataStructures = {
myStack: function () {
var elements = [];
var topOfMyStack = 0;
//Add the push method to the myStack object
this.push = function (e) {
if (typeof (e) !== 'undefined') {
// Push new value.
elements[topOfMyStack++] = e;
}
};
this.pop = function () {
if (topOfMyStack > 0) {
return elements[--topOfMyStack];
}
};
return this;
}
}
var something;
var stack = new dataStructures.myStack();
stack.push(1);
stack.push(something); // undefined
alert(stack.pop()); // 1
相关文章:
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- JSON偶尔未定义-我该如何检查
- setInterval调用具有未定义参数的函数
- JSHint 错误:未定义我的应用程序 (W117)
- 在 javascript 中返回未定义值的单选按钮
- 更改 Javascript 对象中所有未定义变量的值
- 使用 RESTful API,如果未定义特定的响应 json 级别,我如何在客户端不出错
- 无法仅在活动服务器上读取未定义错误的属性“top”
- 查找未定义值的所有对象
- 未定义类型的数据没有值
- 具有null或未定义值的JavaScript字符串串联行为
- 检查链接对象形式中未定义属性的最佳实践是什么
- RequireJS 2.0-定义我的Backbone.Return
- 单击未定义数量的对话框
- 网络浏览器正在成为“;严格的“;用于Javascript中未定义变量的操作
- $ 未在我的 JavaScript 控制台中定义
- 函数未定义..我的函数调用不起作用
- 为什么我在chrome中得到未定义函数的错误
- 为什么我得到“未捕获的类型错误:无法读取未定义(...)的属性'长度'".
- 为什么我得到未定义错误的属性