JQuery function not returning

(function ($) {

    $.a.b  = {

        title: "ABC",

        init: function (id) {
                 /* do something here */
                  return id+'a';

              }




    };

})(jQuery);

      

When I try to call $ .abinit ('t'); it doesn't work, i mean it doesn't return as expected. Any suggestions?

The problem isn't that $ .abinit ('t') doesn't work. The problem is that it returns the entire function code instead of returning a string.

Thank you for your time.

+2


source to share


2 answers


try

$.a = [];
$.a.b  = { ... }

      

or even better:



$.a = {b: {
     title: "",
     init: ...
}};

      

There $.a.b

is undefined when using a, so you cannot add to it.

+7


source


Since it is not yet defined, you cannot set the property . First you need to create . Alternatively, use the namespacing helper: $.a

b

$.a

$.namespace = function(ns) {
    var cur = $, split = ns.split('.');
    while (split[0]) {
        cur = cur[split.shift()] = {};
    }
    return cur;
};

$.namespace('a').b = { ... };

      



It can also be used with deeper namespaces:

$.namespace('a.b.c.d.e.f').g = 123;
$.a.b.c.d.e.f.g; // => 123

      

+4


source







All Articles