开发者

creating a jquery plugin on the jquery core $ object

I would like to create a plugin for jquery that doesn't work with the dom. I therefore don't need to use a jquery object from the $ function. I would like to make my own function called

$.shortcut(keys,action)

Icould probably just go

$.prototype.sho开发者_如何学Gortcut = function(){//my code}

but I would like to know if that's the best way to go about this.


You just about have it already...

$.shortcut = function(keys, action) {
    // code
}


I prefer to wrap mine in an anon function and pass jQuery as a param though. It helps avoid naming conflicts and makes for easier minification.

(function($){
    $.shortcut = function(keys, action) {
        // code
    }
})(jQuery);


Drop the .prototype part, and then it'll the way it's usually done. (except usually you use jQuery instead of $ in case the person is using noConflict)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜