JS移除匿名函数

I believe that is the point of an anonymous function, it lacks a name or a way to reference it.

If I were you I would just create a named function, or put it in a variable so you have a reference to it.

var t = {};
var handler = function(e) {
    t.scroll = function(x, y) {
        window.scrollBy(x, y);
    };
    t.scrollTo = function(x, y) {
        window.scrollTo(x, y);
    };
};

You can then remove it by

window.document.removeEventListener("keydown", handler);  

转自StackOverflow

posted @ 2019-08-20 18:36  marvelousone  阅读(475)  评论(0编辑  收藏  举报