namespace property in jQuery is used to return the custom namespace whenever the event is triggered. It is used to handle tasks differently depending on the namespace used. Syntax: event.namespace. Parameters: This property contains single parameter event which is required.
JavaScript does not provide namespace by default. However, we can replicate this functionality by making a global object which can contain all functions and variables.
The event. namespace property returns the custom namespace when the event was triggered. This property can be used by plugin authors to handle tasks differently depending on the namespace used. Tip: Namespaces beginning with an underscore are reserved for jQuery.
lpfavreau offers the solution to extend the jQuery object with your own methods (so that their functionality applies on the actual jQuery object context).
If you're looking to just namespace your code you can use the dollar symbol like this:
$.myNamespace = { .. };
or the "jQuery":
jQuery.myNamespace = { .. };
Be careful with the namespace you choose as this can overwrite existing jQuery methods (I'd suggest to first search in the jQuery code so that you don't).
You can follow this link: http://www.zachleat.com/web/2007/08/28/namespacing-outside-of-the-yahoo-namespace/
See how easy it is to create your own function to replicate what YUI does:
// include jQuery first.
jQuery.namespace = function() {
var a=arguments, o=null, i, j, d;
for (i=0; i<a.length; i=i+1) {
d=a[i].split(".");
o=window;
for (j=0; j<d.length; j=j+1) {
o[d[j]]=o[d[j]] || {};
o=o[d[j]];
}
}
return o;
};
// definition
jQuery.namespace( 'jQuery.debug' );
jQuery.debug.test1 = function()
{
alert( 'test1 function' );
};
jQuery.debug.test2 = function()
{
alert( 'test2 function' );
};
// usage
jQuery.debug.test1();
jQuery.debug.test2();
This is how I create namespaces for my plugins:
(function ($) {
// do not overwrite the namespace, if it already exists
$.MyNamespace = $.MyNamespace || {};
$.MyNamespace.MyPlugin = function () {/*here's the logic*/}
})($);
And then:
$.MyNamespace.MyPlugin ();
jQuery has a bunch of plugins that extend the base functionality. There is this plugin for easy namespaces.
The namespace plugin is COMPLETELY UNNECESSARY! The oldest trick in the world, the use of arguments.callee, Function.prototype and then calling a new Function instance, allows you to create nested namespaces with $.fn.extend!
Here is a plain and simple example:
;(function($){
var options= {
root: function(){ //you don't have to call it 'root', of course :)
//identify the function from within itself with arguments.callee
var fn= arguments.callee;
//'this' at this level is the jquery object list matching your given selector
//we equate fn.prototype to this
//thus when we call a new instance of root, we are returned 'this'
fn.prototype= this;
fn.value= function(){
//Note: "new this" will work in the below line of code as well,
//because in the current context, 'this' is fn;
//I use fn to make the code more intuitive to understand;
var context= new fn;
console.log(context, fn.prototype); //test
return context.html(); //test
}
return this;
}
}
//you can obviously append additional nested properties in this manner as well
options.root.position= function(){
var context= new this; //in this context, fn is undefined, so we leverage 'this'
console.log(context, this.prototype); //test
return context.offset(); //test
}
//don't forget to extend in the end :)
$.fn.extend(options);
})(jQuery);
;$(function(){
var div= $('div#div')
.root();
console.log(div.root.value());
console.log(div.root.position());
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With