Created
February 27, 2018 09:40
-
-
Save Calerme/126628ba2382e71a43d3331a1bfba2b0 to your computer and use it in GitHub Desktop.
动态创建命名空间
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
var MyApp = {}; | |
MyApp.namespace = function (name) { | |
var parts = name.split('.'); | |
var current = MyApp; | |
for (var i in parts) { | |
if ( !current[ parts[i] ] ) { | |
current[ parts[i] ] = {}; | |
} | |
current = current[ parts[i] ]; | |
} | |
}; | |
MyApp.namespace( 'event' ); | |
MyApp.namespace( 'dom.style' ); | |
// 上述代码等价于 | |
var MyApp = { | |
event: {}, | |
dom: { | |
style: {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment