Is it possible to create a namespace in jQuery?

后端 未结 8 961
星月不相逢
星月不相逢 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条回答
  •  猫巷女王i
    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
    

提交回复
热议问题