我有这个代码:
var history = {
stack : [],
counter : -1,
add : function(item){
this.stack[++this.counter] = item;
this.doSomethingWith(item);
// delete anything forward of the counter
this.stack.splice(this.counter+1);
},
undo : function(){
this.doSomethingWith(this.stack[--this.counter]);
},
redo : function(){
this.doSomethingWith(this.stack[++this.counter]);
},
doSomethingWith : function(item){
// show item
}
};
当我尝试以这种方式使用它时,它不起作用;
var item = $('#myDiv');
history.add(item);
我有这个错误:
Uncaught TypeError: undefined is not a function
如何解决这个问题?谢谢!
最佳答案
问题是您为对象选择了一个错误的名称。有内置的 History
对象可用作 window.history
。因此,将您的名称重命名为其他名称,它应该可以工作。例如:
var appHistory = {
stack : [],
counter : -1,
...
};
例如,您还可以通过将代码包装到 IIFE 中来修复此问题,以便 history
将成为局部变量。但我仍然建议选择不太容易混淆的名称。
关于javascript - 调用jquery对象函数调用history,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25034334/