Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery plugin public method/function

I am trying to achieve something like the following but dont know whats wrong:

$.a = function() {

// some logic here

function abc(id) {
   alert('test'+id);
}


}

$.a.abc('1');

I tried using the return function, but that doesnt seem to work either. Can someone please help.

Thank you for your time.

like image 728
Alec Smart Avatar asked Dec 29 '25 22:12

Alec Smart


1 Answers

Since $.a must be a function in itself, you'll have to add the abc function as a property to the $.a function:

$.a = function () {
    // some logic here...
};

$.a.abc = function (id) {
    alert('test' + id);
};

If abc must be defined from within the $.a function, you can do the following. Do note that $.a.abc will not be available until $.a has been called when using this method! Nothing inside a function is evaluated until a function is called.

$.a = function () {

    // Do some logic here...

    // Add abc as a property to the currently calling function ($.a)
    arguments.callee.abc = function (id) {
        alert('test' + id);
    };
};

$.a();
$.a.abc('1');
like image 174
Blixt Avatar answered Dec 31 '25 11:12

Blixt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!