Is it possible to create a namespace in jQuery?

后端 未结 8 751
星月不相逢
星月不相逢 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 13:57

    If you want to use jQuery in this manner:

    $("*").namespace.do();
    

    then currently there're no plugins doing that. (I can't find neither John Resig's jquery.space plugin which apparently stopped working in jQuery 1.4, nor Gilberto Saraiva plugin that apparently didn't work as expected). I would gladly take a look at John's function to see why it stopped working, and what can be done to make it work and quite frankly, it would be the best approach of creating uncluttered namespaces.

    As per http://malnotna.wordpress.com/2009/01/12/jquery-namespace-support/ another approach is to do namespacing as (using jQuery.Modularize by Ariel Flesler):

    $("*").namespace().do()
    

    but such syntax isn't "pretty". We're also passing results from one function to another.

    My approach to creating namespaces is not putting namespaces in the end, but at the beginning of $, so our $('*').namespace syntax becomes:

    $.namespace("*").do()
    

    Weirdly, I don't know why such approach isn't mentioned, as it easily allows you to create unclattered namespaces without overwriting already existing functions (by using $.sub()). Also, making it work doesn't require anything. So:

    (function($){
        $.namespace = $.sub();
        $.fn.test = function(){ return 1 };              
        $.namespace.fn.test = function() { return 2};
    })(jQuery);
    
    console.log($('*').test(), $.namespace('*').test());
    

    And you're ready to go.

    0 讨论(0)
  • 2020-11-27 14:00

    lpfavreau offers the solution to extend the jQuery object with your own methods (so that their functionality applies on the actual jQuery object context).

    If you're looking to just namespace your code you can use the dollar symbol like this:

    $.myNamespace = { .. };
    

    or the "jQuery":

    jQuery.myNamespace = { .. };
    

    Be careful with the namespace you choose as this can overwrite existing jQuery methods (I'd suggest to first search in the jQuery code so that you don't).

    You can follow this link: http://www.zachleat.com/web/2007/08/28/namespacing-outside-of-the-yahoo-namespace/

    See how easy it is to create your own function to replicate what YUI does:

    // include jQuery first.
    jQuery.namespace = function() {
        var a=arguments, o=null, i, j, d;
        for (i=0; i<a.length; i=i+1) {
            d=a[i].split(".");
            o=window;
            for (j=0; j<d.length; j=j+1) {
                o[d[j]]=o[d[j]] || {};
                o=o[d[j]];
            }
        }
        return o;
    };
    
    // definition
    jQuery.namespace( 'jQuery.debug' );
    jQuery.debug.test1 = function()
    {
        alert( 'test1 function' );
    };
    jQuery.debug.test2 = function()
    {
        alert( 'test2 function' );
    };
    // usage
    jQuery.debug.test1();
    jQuery.debug.test2();
    
    0 讨论(0)
  • 2020-11-27 14:01

    check out this blog: http://javascriptweblog.wordpress.com/2010/12/07/namespacing-in-javascript/

    the self-invoking dynamic namespacing is what i've used before:

    var myApp = {};
    (function(context) {
        var id = 0;
    
        context.next = function() {
            return id++;
        };
    
        context.reset = function() {
            id = 0;
        } 
    })(myApp); 
    
    window.console && console.log(
        myApp.next(),
        myApp.next(),
        myApp.reset(),
        myApp.next()
    ); //0, 1, undefined, 0
    
    0 讨论(0)
  • 2020-11-27 14:03

    This is how I create namespaces for my plugins:

    (function ($) {
        // do not overwrite the namespace, if it already exists
        $.MyNamespace = $.MyNamespace || {};
        $.MyNamespace.MyPlugin = function () {/*here's the logic*/}
    })($);
    

    And then:

    $.MyNamespace.MyPlugin ();
    
    0 讨论(0)
  • 2020-11-27 14:04

    The namespace plugin is COMPLETELY UNNECESSARY! The oldest trick in the world, the use of arguments.callee, Function.prototype and then calling a new Function instance, allows you to create nested namespaces with $.fn.extend!

    Here is a plain and simple example:

        ;(function($){
        var options= {
            root: function(){ //you don't have to call it 'root', of course :)
                //identify the function from within itself with arguments.callee
                var fn= arguments.callee;
    
                //'this' at this level is the jquery object list matching your given selector
                //we equate fn.prototype to this 
                //thus when we call a new instance of root, we are returned 'this'
                fn.prototype= this;
    
                fn.value= function(){
                    //Note: "new this" will work in the below line of code as well,
                    //because in the current context, 'this' is fn;
                    //I use fn to make the code more intuitive to understand;
                    var context= new fn; 
    
                    console.log(context, fn.prototype); //test
                    return context.html(); //test
                }
    
                return this;
            }
        }
    
        //you can obviously append additional nested properties in this manner as well
        options.root.position= function(){
            var context= new this; //in this context, fn is undefined, so we leverage 'this'
    
            console.log(context, this.prototype); //test
            return context.offset(); //test
        }
    
        //don't forget to extend in the end :)
        $.fn.extend(options);
    
    })(jQuery);
    
    ;$(function(){
        var div= $('div#div')
            .root();
    
        console.log(div.root.value());
        console.log(div.root.position());
    });
    
    0 讨论(0)
  • 2020-11-27 14:05

    Depending on what you're trying to do, jQuery's plugin architecture may be what you're looking for:

    $.fn.myPlugin = function() {
      return $(this).each(function() {
        // do stuff
      });
    };
    

    or ...

    $.fn.myPlugin = function() {
      var myNamespace = {
        // your stuff
      };
    };
    

    really it depends on what you're trying to do.

    http://docs.jquery.com/Plugins/Authoring

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