jQuery plugin public method/function

前端 未结 2 412
余生分开走
余生分开走 2021-01-14 05:39

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

$.a = function() {

// some logic here

function abc(id) {
   alert(\'test\'+i         


        
相关标签:
2条回答
  • 2021-01-14 06:28

    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');
    
    0 讨论(0)
  • 2021-01-14 06:33
    $.a = (function(){
        var a = function() {
            //...
        };
        a.abc = function() {
            //...
        }
        return a;
    })();
    
    0 讨论(0)
提交回复
热议问题