I added the following method to the Array 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; }
In the same file, after the above code, I have the following jQuery plugin:
jQuery.fn.addClassForEvents = function(){ var that = this; arguments.foreach(function(event){ that.bind(event[0], function(){ that.addClass(event[0]); }) .bind(event[1], function(){ that.removeClass(event[0]); }); }); return this; }
To use this jQuery plugin, my code would look something like this:
$('div').addClassForEvents(['mouseenter', 'mouseleave']);
However, the browser throws an error in the string "arguments.foreach (...." of the jQuery plugin, simply indicating that
Object # does not have a 'foreach' method
However, the foreach method works elsewhere in my code. Why is it undefined inside this jQuery plugin?
source share