I’m wondering how can I attach a function to be available for each object on the page. I know that I can do it like this:
mything = {
init: function(SELECTOR){
},
destroy: function(){
}
};
But then it is available to me only this way: mything.init(SELECTOR);
What I want is to be able to do the same thing this way:
$('.mydiv, input, whatever').myFunction({ 'my' : 'arg', 'my2' : 'arg2' });
$('.mydiv').myFunction('destroy');
I know that there are plenty of Javascript tutorials out there but I don’t know how to search for this type of functionality. Any help would be appreciated!
In your case, it looks like it is just enough for you to extend the
jQuery.prototype.However, it is of course possible, even if not very recommendable, to go that ultimate root and extend the
Object.prototype. This really would add those functions to every and each object.