Last active
October 12, 2015 12:27
-
-
Save samshull/4026233 to your computer and use it in GitHub Desktop.
Iterative jQuery methods that use a single jQuery object for context to simplify usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function($){ | |
$.fn.$each = function(callback) { | |
var $temp = $([1]); | |
return this.each(function() { | |
$temp[0] = this; | |
return callback.apply($temp, arguments); | |
}); | |
}; | |
$.fn.$filter = function(callback, invert) { | |
var $temp = $([1]); | |
return this.filter(function() { | |
$temp[0] = this; | |
return callback.apply($temp, arguments); | |
}, invert); | |
}; | |
$.fn.$map = function(callback) { | |
var $temp = $([1]); | |
return this.map(function() { | |
$temp[0] = this; | |
return callback.apply($temp, arguments); | |
}); | |
}; | |
$.fn.some = $.fn.is; | |
$.fn.$some = $.fn.$is = function() { | |
return this.$filter.apply(this, arguments).length > 0; | |
}; | |
$.fn.every = function() { | |
return this.length === this.filter.apply(this, arguments).length; | |
}; | |
$.fn.$every = function() { | |
return this.length === this.$filter.apply(this, arguments).length; | |
}; | |
}(jQuery)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment