JavaScript Namespace

来源:互联网 发布:拍网络电影怎么赚钱 编辑:程序博客网 时间:2024/05/20 05:53
           
    JavaScript Namespace        
            
    With the prevailing usage of AJAX calls, multiple subapps (and their corresponding JS snippets) will be loaded in the same logical "page", thus sharing the same runtime namespace.        
    Without proper naming control, there's a high chance of different subapps having functions and variable naming conflicts, when they are "weaved" fo        
    Therefore, it is imperative that we have proper namespace management for our javascript functions, both for common js library, and for subapp js        
            
    All subapps are implicitly importing and inheriting the following "objects"        
            
            
    Javascript my itself does not have the concept of namespace.        
    However, we can achieve the effect of non-conflicting namespace by following a coding pattern.        
    For us, the following pattern is proposed.        
            
    All Javascript fields and function definitions must be defined this way (assuming subapp name is TestSubapp):        
            
    var TestSubapp = new function() {        
        this.var1 = "";    
        this.function1 = function() {    
            return 12345;
        };    
    }        
            
    Instead of placing your variables and functions in the global namespace, a virtual namespace "TestSubapp" is simulated.        
    The virtual namespace will take the naming convention of CamelCase, using the name of the subapp.        
            
    Variables and functions declared this way can be accessed using:        
    TestSubapp.var1 = TestSubapp.function();        
            
            
    The following namespace are provided at framework level, and are automatically available to subapps        

原创粉丝点击