Is it possible to create a namespace in jQuery?

后端 未结 8 752
星月不相逢
星月不相逢 2020-11-27 13:52

YUI has a nice way of creating a namespace for your methods etc. in javascript.

Does jQuery have anything similiar?

相关标签:
8条回答
  • 2020-11-27 14:10

    All credit to @Diego Fluery, I just took his slide deck and made a runnable code prototype, but it might save you a few minutes if you go this route:

    <!DOCTYPE html>
    <html>
    <head>
    <script type="application/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
    <script type="application/javascript">
    
        $(document).ready(function() {
            (function () {
                $.fn.AppRedirect = function() {
                    this.sendRequest = parts.sendRequest;
                    this.doRedirect = parts.doRedirect;
    
                    return this;
                }
    
                var parts = {
    
                    doRedirect: function() {
                        console.log('doRedirect');
                    },
    
                    sendRequest: function() {
                        console.log('sendRequest');
                    }
    
                };
            })();
    
            $("body").AppRedirect().sendRequest();
            $("body").AppRedirect().doRedirect();
        });     
    
    </script>
    </head>
    <body>
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-27 14:15

    jQuery has a bunch of plugins that extend the base functionality. There is this plugin for easy namespaces.

    0 讨论(0)
提交回复
热议问题