JAVASCRIPT:尝试在自定义事件调度器上创建hasCallbackFor函数

JAVASCRIPT:尝试在自定义事件调度器上创建hasCallbackFor函数,javascript,event-handling,event-listener,Javascript,Event Handling,Event Listener,我正在做一个创建自定义事件调度器的练习 我得到了一堆测试用例,我一直在为我的事件侦听器对象创建一个名为hascallbackborname的方法,callback。据我所知,hasCallbackFor函数应该采用我正在创建的对象上的一个键的名称,而callback是一个位于该键的数组中的函数。它应该检查这个函数是否存在。我完全不知道该怎么做,感觉自己什么都试过了 这是hasCallbackFor函数的测试用例: var shouldReturnFalseHasCallBackForIfMeth

我正在做一个创建自定义事件调度器的练习

我得到了一堆测试用例,我一直在为我的事件侦听器对象创建一个名为hascallbackborname的方法,callback。据我所知,hasCallbackFor函数应该采用我正在创建的对象上的一个键的名称,而callback是一个位于该键的数组中的函数。它应该检查这个函数是否存在。我完全不知道该怎么做,感觉自己什么都试过了

这是hasCallbackFor函数的测试用例:

var shouldReturnFalseHasCallBackForIfMethodsNotAdded = function () {
    testObj = {};
    var scope = {
        executeSuccess: true
    }
    var testFunction = function () {
        if (this.executeSuccess) {
            success1 = true;
        }
    }
    EventDispatcher.mixin(testObj);
    testObj.addEventListener('test', testFunction, scope);
    testObj.dispatchEvent('test');
    assert(testObj.hasCallbackFor("test", testFunction), 'should have callback registered for test event');
    assert(!testObj.hasCallbackFor("test", function () {}), 'should have no callback');
    console.log('shouldReturnFalseHasCallBackForIfMethodsNotAdded: success')
}
这是我的addEventListener函数:

/**
 * @param {string} name
 * @param {function} callback
 * @param {Object} opt_scope
 */
addEventListener: function (name, callback, opt_scope) {
    if(!EventDispatcher.myObject[name]) {
        EventDispatcher.myObject[name] = [];
    }
    if(opt_scope) {
        var bound = callback.bind(opt_scope);
        EventDispatcher.myObject[name].push(bound);
    } else {
        EventDispatcher.myObject[name].push(callback);
    }
},
/**
 * @param {string} name
 */
dispatchEvent: function (name) {
    EventDispatcher.myObject[name].forEach(function(value) {
       value();
    });
},
这是我的dispatchEvent函数:

/**
 * @param {string} name
 * @param {function} callback
 * @param {Object} opt_scope
 */
addEventListener: function (name, callback, opt_scope) {
    if(!EventDispatcher.myObject[name]) {
        EventDispatcher.myObject[name] = [];
    }
    if(opt_scope) {
        var bound = callback.bind(opt_scope);
        EventDispatcher.myObject[name].push(bound);
    } else {
        EventDispatcher.myObject[name].push(callback);
    }
},
/**
 * @param {string} name
 */
dispatchEvent: function (name) {
    EventDispatcher.myObject[name].forEach(function(value) {
       value();
    });
},
对于hasCallbackFor函数,我尝试使用

/**
 * @param {string} name
 * @param {function} callback
 * @return {boolean}
 */
hasCallbackFor: function (name, callback) {
    return EventDispatcher.myObject[name].includes(callback);
},
此函数在测试时使测试用例失败

 assert(testObj.hasCallbackFor("test", testFunction), 'should have callback registered for test event');
行,我已经没有什么好主意了。我已经盯着这段代码看了大约3个小时了,如果您能对此有所了解,我将不胜感激。谢谢大家!

函数名testFunction在本例中是代码的地址。 让我们考虑一个小例子:

var arr = [];
var foo = function(e){return true;};
arr.push(foo);
console.log(arr.includes(foo));//true
var bar = foo.bind('something');
arr.push(bar);
arr.includes(bar);//true
arr.includes(foo.bind('something'));//false .bind creates a new function every time
//The worst case:
arr.push(function(e){return true;});//anonymous function is lost forever
console.log(arr.includes(function(e){return true;}));//false
回到OP,问题就在这里:

var bound = callback.bind(opt_scope);//creates a new function with a new address
EventDispatcher.myObject[name].push(bound);
我提供两种解决方案: 从addEventListener返回函数

在shouldReturnFalshasCallbackForifMethodNotAdded中,调用如下函数:

var foo = testObj.addEventListener('test', testFunction, scope);
//  ^^^
testObj.dispatchEvent('test');
assert(testObj.hasCallbackFor("test", foo), 'should have callback registered for test event');
//                                    ^^^
绑定shouldReturnFalshasCallbackForifMethodsNotAdded中的函数,并且不将作用域发送到addEventListener


两种方法都有效。

非常感谢您的帮助和详细解释!我知道它与指向不同事物的绑定函数有关,只是不知道如何解决它。再次感谢你!