This method extends the jQuery prototype ($.fn) object to provide new custom methods that can be chained to the jQuery() function.
For example:
<div>Hello</div>
<div>World!</div>
<script>
jQuery.fn.extend({
// returns combination of div texts as a result
getMessage: function() {
var result;
// this keyword corresponds result array of jquery selection
this.each(function() {
// $(this) corresponds each div element in this loop
result = result + " " + $(this).val();
});
return result;
}
});
// newly created .getMessage() method
var message = $("div").getMessage();
// message = Hello World!
console.log(message);
</script>