V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
withrock
V2EX  ›  JavaScript

谁能看看这段 javascript 代码是什么含义?实现了什么功能?

  •  
  •   withrock · 2016-06-29 23:09:21 +08:00 · 1864 次点击
    这是一个创建于 3060 天前的主题,其中的信息可能已经有所发展或是发生改变。
    var Namespace=(function(windows){
    	var _toArray = function(obj) {
    		// checks if it's an array
    		if (typeof(obj) == 'object' && obj.sort) {
    			return obj;
    		}
    		return new Array(obj);
    	};
    	var _namespace = function(identifier) {
    		var klasses = arguments[1] || false;
    		var ns = windows;
    		
    		if (identifier != '') {
    			var parts = identifier.split(Namespace.separator);
    			for (var i = 0, j = parts.length; i < j; i++) {
    				if (!ns[parts[i]]) {
    					ns[parts[i]] = {};
    				}
    				ns = ns[parts[i]];
    			}
    		}
    		
    		if (klasses) {
    			for (var klass in klasses) {
    				ns[klass] = klasses[klass];
    			}
    		}
    		
    		//_dispatchEvent('create', { 'identifier': identifier });
    		return ns;
    	};
    	_namespace.use = function(identifier,isglobal) {
    		var _varscope =isglobal?windows:{};
    		var identifiers 		= _toArray(identifier);
    		var callback 			= arguments[1] || false;
    		
    		for (var i = 0, j = identifiers.length; i < j; i++) {
    			identifier = identifiers[i];
    		
    			var parts = identifier.split(Namespace.separator);
    			var target = parts.pop();
    			var ns = _namespace(parts.join(Namespace.separator));
    		
    			if (target == '*') {
    				// imports all objects from the identifier, can't use include() in that case
    				for (var objectName in ns) {
    					_varscope[objectName] = ns[objectName];
    				}
    			} else {
    				// imports only one object
    				if (ns[target]) {
    					// the object exists, import it
    					_varscope[target] = ns[target];
    				}
    			}
    		
    		}
    		return _varscope;
    	};
    	return _namespace;
    })(window)
    $.ajaxSetup({
      cache: true
    });
    Namespace.separator = '.';
    
    

    使用方式是:

    var _Constant = Namespace('all.constant');
        require(['commonutil'], function(ajaxUtil) {
            ajaxUtil.callrest('/bases/v1/sso/token', function(data) {
                if (data.code == "000000") {
                    var _Constant = Namespace('all.constant');
                    _Constant.TOKEN = data.data.TOKEN;
                }
            }, {}, false);
        });
    
    
    3 条回复    2016-07-04 18:35:47 +08:00
    MinonHeart
        1
    MinonHeart  
       2016-06-30 10:58:50 +08:00
    相当于这样
    require(['commonutil'], function(ajaxUtil) {
    ajaxUtil.callrest('/bases/v1/sso/token', function(data) {
    if (data.code == "000000") {
    window.all = window.all || {};
    window.all.constant = window.all.constant || {};
    var _Constant = window.all.constant;
    _Constant.TOKEN = data.data.TOKEN;
    }
    }, {}, false);
    });
    withrock
        2
    withrock  
    OP
       2016-07-01 12:03:42 +08:00
    @MinonHeart 请问这样做的目的是什么?是防止污染全局命名空间么?
    MinonHeart
        3
    MinonHeart  
       2016-07-04 18:35:47 +08:00
    @withrock 方便创建深层次的对象
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5703 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 01:39 · PVG 09:39 · LAX 17:39 · JFK 20:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.