jQuery Plugin Namespace

后端 未结 5 1517
温柔的废话
温柔的废话 2020-12-23 18:18

How do I create a jQuery plugin so that I can use namespaces in my plugin ?

$(\"#id1\").amtec.foo1();
$(\"#id1\").amtec.foo2();

None of th

5条回答
  •  生来不讨喜
    2020-12-23 18:56

     $.cg = {
      foo1: function(weq){
          return console.log(weq);
      },
      foo2: function(rw){
          return console.log(rw);
     }
    }; 
    $.cg = { // will result in error because $.cg is already declared above
    
      foo4: function(rw){ // will result in error
          return console.log(rw); // will result in error
     } // will result in error
    }; // will result in error
    
    $.cg.foo3 = function(weq){ //to add anything new to $.cg , you have to do it this way.
          return console.log(weq);
      }
    
    $.cg.foo1("12");
    $.cg.foo2("22"); //works fine.
    $.cg.foo3("112"); //works fine.
    

提交回复
热议问题