Defining your own functions in jQuery
Defining your own functions in jQuery.
In this post i want to present how easy define your own functions in jQuery and using them.
As you know, jQuery it's a very rich JavaScript library and it's a pity not to use all it resources.
Ok. Do you want to create your own jQuery function to work with elements look like this:
$(element).yourfunctionname()
What is your steps to define it?
At first, define "jQuery.fn" and add to this object your function name see below. (jQuery.fn is a prototype of jQuery object)
jQuery.fn.yourfunctionname = function() {
var o = $(this[0]) // It's your element
};
Now you have your own jQuery function working with HTML elements.
Also you want to call this function and set any attributes see below.
$(elem).yourfunctionname({dataSource: [], pagingStart: 5});
jQuery.fn.yourfunctionname = function() {
var args = arguments[0] || {}; // It's your object of arguments
var dataSource = args.dataSource;
var pagingStart = args.pagingStart;
};
Extend jQuery using your own functions.
Come to say... You want to create function for validation object or variable to is nullable (
null).
Like this:
if( !jQuery.isNull(object) )
Do next
jQuery.extend( {
isNull: function(o) {
if(o == null && o == undefined) {
return true;
}
return false
}
});
Or. You want to extend someone your own jQuery function
jQuery.fn.yourfunctionname.constructor.prototype.clearData = function() {
// TODO
};
Enjoy...