Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么对数组原型的这种更改在我的jQuery插件中不起作用?_Javascript_Jquery_Arrays_Jquery Plugins_Prototype - Fatal编程技术网

Javascript 为什么对数组原型的这种更改在我的jQuery插件中不起作用?

Javascript 为什么对数组原型的这种更改在我的jQuery插件中不起作用?,javascript,jquery,arrays,jquery-plugins,prototype,Javascript,Jquery,Arrays,Jquery Plugins,Prototype,我在阵列原型中添加了以下方法: Array.prototype.foreach = function(func){ for(var i = 0; i < this.length; i++){ if(!func(this[i]) === false) break; //return false from func in order to break the loop } return this; } 为了使用这个jQuery插件,我的代码如下所示:

我在阵列原型中添加了以下方法:

Array.prototype.foreach = function(func){
    for(var i = 0; i < this.length; i++){
        if(!func(this[i]) === false) break; //return false from func in order to break the loop
    }
    return this;
}
为了使用这个jQuery插件,我的代码如下所示:

$('div').addClassForEvents(['mouseenter', 'mouseleave']);
但是,浏览器在jQuery插件的“arguments.foreach(..”行上抛出一个错误,简单地说

对象#没有方法“foreach”


然而,
foreach
方法在我的代码的其他地方也可以使用。为什么在这个jQuery插件中它没有定义?

它不起作用,因为参数不是数组。它是一个(类似数组的)参数对象


您可以在现代浏览器中使用slice将其转换为数组(并在IE中实际循环)


arguments
不是数组,而是对象。例如,它提供属性,如
arguments.callee
arguments.caller

您可以通过对其调用
apply
(cf.)来使用阵列原型的foreach:

由于Array.prototype的所有方法都设计为通用方法,因此它们可以轻松应用于与数组兼容的arguments对象:


您需要将参数对象转换为数组

试试这个:

jQuery.fn.addClassForEvents = function(){

    var that = this, arg = Array.prototype.slice.call(arguments);

    arg.foreach(function(event){
        that.bind(event[0], function(){
            that.addClass(event[0]);
        })
        .bind(event[1], function(){
            that.removeClass(event[0]);
        });
    });

    return this;
}

要将
参数
转换为数组,您也可以使用
jQuery.makeArray(参数)


当函数返回一个truthy值时,为什么要中断循环(和foreach)?这实际上是一个错误(请看我在那行代码旁边的注释!)。感谢您指出。这在foreach函数/方法中仍然没有多大意义…为什么?似乎提供一种打破循环的方法是相当标准的。一般理解是foreach迭代集合的每个元素。我不知道有哪种语言,如果应用的函数n返回false。请随意证明我错了。您可以使用IE中的slice将其转换为数组too@AngusC你可能是对的。我有一个通用的函数来实现这一点,而且肯定有一些类似数组的对象,IE不喜欢调用slice。
var argArray = Array.prototype.slice.call(arguments)
jQuery.fn.addClassForEvents = function(){

    var that = this;

    [].foreach.apply(arguments, (function(event){
        that.bind(event[0], function(){
            that.addClass(event[0]);
        })
        .bind(event[1], function(){
            that.removeClass(event[0]);
        });
    });

    return this;
}
jQuery.fn.addClassForEvents = function(){

    var that = this, arg = Array.prototype.slice.call(arguments);

    arg.foreach(function(event){
        that.bind(event[0], function(){
            that.addClass(event[0]);
        })
        .bind(event[1], function(){
            that.removeClass(event[0]);
        });
    });

    return this;
}