Last active
December 15, 2015 02:29
-
-
Save monjer/5187436 to your computer and use it in GitHub Desktop.
JS对象命名空间管理器
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Date 2012.7.26 | |
| * @param {Object} opt_ns 命名空间 | |
| * @para {DOMWindow} opt_wind 浏览器BOM window | |
| */ | |
| (function(opt_ns , opt_wind){ | |
| var g_win = opt_wind || top ; | |
| var g_doc = g_win.document ; | |
| var toString = Object.prototype.toString ; | |
| //非空字符串判断 | |
| function isStringAndNotEmpty(value){ | |
| return toString.call(value) === '[object String]' && !/^\s+$/.test(value) ; | |
| } | |
| var namespace = { | |
| /** | |
| * 命名空间实现的记录 | |
| */ | |
| implementNs:{}, | |
| /** | |
| * 判断是否一存在该命名空间 | |
| * @param {Object} namespace 命名空间的路径名,如 app.view.button | |
| */ | |
| _isExsited:function(namespace){ | |
| if(this.implementNs[namespace]){ | |
| return true ; | |
| }else{ | |
| return false ; | |
| } | |
| }, | |
| /** | |
| * 创建指定的命名空间,并将opt_object挂载到最后的路径下 | |
| * @param {Object} namespace 命名空间路径 "a.b.c" | |
| * @param {Object} opt_object 可选对象 ,挂载到命名空间的末尾 | |
| * @param {Object} opt_objectToExportTo 命名空间的目标对象,默认为window | |
| */ | |
| create:function(namespace, opt_object){ | |
| if (typeof namespace !== "string") { | |
| throw new Error("ns.create error : Namespace not a string"); | |
| return ; | |
| }else if(this._isExsited(namespace)){ | |
| throw new Error("ns.create error : Namespace exist"); | |
| return ; | |
| }else { | |
| var currentObj = g_win; | |
| var tmplns = namespace; | |
| while((tmplns = tmplns.substring(0 , tmplns.lastIndexOf(".")))){ | |
| if(this.getObjByNs(tmplns)){ | |
| break ; | |
| } | |
| this.implementNs[tmplns] = true ; | |
| } | |
| //export | |
| var names = namespace.split("."); | |
| var length = names.length; | |
| for (var i = 0; i < length; i++) { | |
| if (i == length - 1 && opt_object) { | |
| currentObj[names[i]] = opt_object; | |
| }else if (currentObj[names[i]]) { | |
| currentObj = currentObj[names[i]]; | |
| }else { | |
| currentObj = currentObj[names[i]] = {}; | |
| } | |
| } | |
| return currentObj; | |
| } | |
| }, | |
| /** | |
| * 获取指定命名空间对象 | |
| * @param {Object} namespace 命名空间路径 | |
| * @param {Object} opt_srcObj | |
| */ | |
| getObjByNs:function(namespace , opt_srcObj){ | |
| if( !isStringAndNotEmpty(namespace) ){ | |
| return null ; | |
| }else{ | |
| var cur = opt_srcObj || g_win ; | |
| var ns = namespace.split(".") ; | |
| for(var length = ns.length , i = 0 ;i < length ; i++){ | |
| if(cur[ns[i]] !== null){ | |
| cur = cur[ns[i]] | |
| }else{ | |
| return null ; | |
| } | |
| } | |
| return cur; | |
| } | |
| } | |
| } | |
| opt_ns = opt_ns || g_win; | |
| opt_ns.ns = opt_ns.namespace = namespace ; | |
| })(undefined , window) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html"> | |
| <html> | |
| <head> | |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> | |
| <title>ns Test</title> | |
| <script src="ns.js"></script> | |
| </head> | |
| <body> | |
| <script> | |
| var appViewReturned = ns.create("app.view"); | |
| appViewReturned.id = 'uid' ; | |
| var appViewGeted = ns.getObjByNs("app.view"); | |
| console.log(appViewGeted.id); // output : uid | |
| //ns.create("app"); | |
| </script> | |
| </body> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment