Last active
August 29, 2015 14:08
-
-
Save rochnyak-d-i/f3a141431458f55772ff to your computer and use it in GitHub Desktop.
JS Пространства имен
This file contains 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
function namespace(ns_string) { | |
var | |
parts = ns_string.split('.') | |
, parent = MYAPP | |
, i | |
, length | |
; | |
// отбросить начальный префикс – имя глобального объекта | |
if (parts[0] === 'MYAPP') { | |
parts = parts.slice(1); | |
} | |
length = parts.length; | |
for (i = 0; i < length; i += 1) { | |
// создать свойство, если оно отсутствует | |
if (typeof parent[parts[i]] === 'undefined') { | |
parent[parts[i]] = {}; | |
} | |
parent = parent[parts[i]]; | |
} | |
return parent; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment