开发者

How to create a catch all function on a jQuery object?

I was previously using the jQuery autosave p开发者_高级运维lugin but have recently removed it. Some code is still trying to use this function, so how can I temporarily extend the jQuery object so there is an empty function to prevent errors until I can get through all of the code that is trying to use the function?

The plugin is called in this way:

jQuery().autosave.restore();
jQuery().autosave.save();

I think those are the only two functions that exist, so it would be OK to create two empty functions, but is there a way to create a catch-all function on this type of object?

Note

This is a temporary solution until I can go through a lot of code. I do believe the question is a valid coding question even if you think this workaround is not ideal (it isn't).


There is a way to do this. You can create a dummy plugin (check out jQuery's documentation for creating plugins):

(function( $ ){

  $.fn.autosave = {
       restore: function() {};
       save: function() {};
  };
})( jQuery );

I would highly recommend against doing this, however. Instead, you should look at those errors and fix them, i.e., stop your code from using them. Otherwise you're simply hiding the problem.


Nope. Standard JavaScript does not support "catch-all" methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜