For example I have this plugin code:
jQuery.fn.keys = function(obj) {
var keys = [];
$.each(obj,function(i,elem) {
keys.push(i);
});
return keys;
};
I’d like to apply this plugin in such a way:
var a = { 'a':1,'b':23,'c':43};
var b = $.keys(a); // should return ['a','b','c']
The above code returns an error.
How would I call the jQuery plugin as a method of the jQuery object; $.key() and not $('#elem').key()?
$.fnis used when adding methods to be called on jQuery result objects.For example, creating
$.fn.extendwould allow you to call something like$('#some_el').extend().To extend
$instead, use$.extend: