Extend $ fn namespace in jQuery

It seems simple, but I can't figure out how to expand the $ fn namespace.

JS:

(function ($) { $.fn.myorg.level1.level2.myFunction = function (usettings, params) { return this.each(function () { //do work...etc }); }; } (jQuery)); $("bla").myorg.level1.level2.myFunction(...); 

How to define a namespace chain so that I can write functions like above?

The example I gave is very simple, the namespace is not expected to be very deep, but I need to be able to extend and add functions ... etc. into my name tree optionally. I do not want to redefine the tree for each function / level added.

I have not seen good examples. If I stay at the $ fn level, itโ€™s easy, but it doesnโ€™t make my code as clean and extensible as I need it.

If what I ask is impossible, what is the right approach?

+4
source share
1 answer

Epascarello hinted at this, but I assume that the previous objects are not defined.

 console.log($.fn.myorg); // undefined $.fn.myorg.level1 = function(){}; // TypeError: Cannot set property 'level1' of undefined 

But if you first define things:

 $.fn.myorg = $.fn.myorg || {}; // if this object doesn't exist, create an blank object console.log($.fn.myorg); // object $.fn.myorg.level1 = function(){}; console.log($.fn.myorg.level1); // function 
+1
source

All Articles