1
$.fn.myTest=function() {
 var externalfunc=function(){
  alert('haii'); 
 }
};

$.fn.myTest.sample=function(){
 var self = this;
 self.myTest.externalfunc();
}

Can someone tell me how to correct this?

1 Answer 1

1

I have no clue what you are trying to do here. If you extend the jQuery .fn. namespace, all jQuery objects inherit that propertys via prototypal inheritance.

jQuery.fn.myTest = function() {
   var foobar = function(){
       alert('yay');
   };
};

That means $('somelement').myTest() is now available, but you cannot access foobar() here since foobar() is a private member of myTest() (-> function scope). So is your externalfunc().

You could do something like

jQuery.fn.myTest = function() {
   return (function(){
       alert('yay');
   }());
};

That now would really alert on jQuery('element').myTest(), but I doubt that is what you want to achieve. You always should return this within a .fn. method, to preserve the object chain.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.