-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnamespace.js
More file actions
40 lines (33 loc) · 950 Bytes
/
namespace.js
File metadata and controls
40 lines (33 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* 对象创建模式:命名空间模式 + 依赖声明模式、沙箱模式、method()方法
*/
/*
* 命名空间模式
*/
var MYAPP = MYAPP || {};
MYAPP.namespace = function(ns_string) {
var parts = ns_string.split('.'),
parent = MYAPP,
i;
// 去除不必要的全局变量层
// 译注:因为namespace已经属于MYAPP
if (parts[0] === "MYAPP") {
parts = parts.slice(1);
}
for (i = 0; i < parts.length; i += 1) {
// 如果属性不存在则创建它
if (typeof parent[parts[i]] === "undefined") {
parent[parts[i]] = {};
}
parent = parent[parts[i]];
}
return parent;
};
// demo
// 将返回值赋给本地变量
var module2 = MYAPP.namespace('MYAPP.modules.module2');
module2 === MYAPP.modules.module2; // true
// 省略全局命名空间`MYAPP`
MYAPP.namespace('modules.module51');
// 长命名空间
MYAPP.namespace('once.upon.a.time.there.was.this.long.nested.property');