javascript - 调用jquery对象函数调用history

标签 javascript jquery

我有这个代码:

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/

相关文章:

javascript - 未捕获的类型错误 : data[option] is not a function bootstrap. js:1215

javascript - 如何处理对象数组?我有一个包含 3 个对象的数组,其中包含来 self 的数据库的 JSON 数据

javascript - 在jquery中获取div的值?

javascript - X 次访问后隐藏 div(cookie)

javascript - 未捕获的类型错误 : Cannot read property 'toUpperCase' of undefined

javascript - 如何使用 JavaScript/jquery 暂停或延迟动画

javascript - 安装 MEAN 堆栈时出现问题

javascript - 我如何使用 jquery 或 javascript 验证日期、月份和年份的 3 个单独输入字段?

javascript - jQuery 特效组合

jquery - jQuery ajax 应用程序和 tomcat 服务器的 CORS 实现/启用?