1

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.shortcut = function(){//my code}

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

2 Answers 2

3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

This would also work best in a closure. This way you can reference the jQuery object as $ and know that it will work with noConflict: docs.jquery.com/Plugins/Authoring
1

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)

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.