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);
});
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.