Skip to content

Instantly share code, notes, and snippets.

@Calerme
Created February 27, 2018 09:40
Show Gist options
  • Save Calerme/126628ba2382e71a43d3331a1bfba2b0 to your computer and use it in GitHub Desktop.
Save Calerme/126628ba2382e71a43d3331a1bfba2b0 to your computer and use it in GitHub Desktop.
动态创建命名空间
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