阅读背景:

IndexOf不能正确处理包含数组的对象

来源:互联网 

I have a the next code:

我有下一个密码:

function insertIfNotThere(array, item) {
    /* Push the new element everytime: because return -1 */
    if (array.indexOf(item) === -1) {
        array.push(item);
    }
    /*But this log shows 1, 2, 3 and so on...*/
    console.log(array.length);

}

function EventManager(target) {
    var target = target || window, events = {};
    this.observe = function(eventName, cb) {
        if (events[eventName]){
           /* This insert the new event everytime: indexOf doesn't work... */
           insertIfNotThere(events[eventName], cb);
        }else{
           events[eventName] = []; events[eventName].push(cb);
        }
        return target;
    };

    this.fire = function(eventName) {
        if (!events[eventName]) return false;
        for (var i = 0; i < events[eventName].length; i++) {
        events[eventName][i].apply(target, Array.prototype.slice.call(arguments, 1));
        }
    };
}
function i



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: