<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <script> var YourGlobal = { namespace: function(ns) { var parts = ns.split('.'), object = this, i, len; for (i = 0, len = parts.length; i < len; i++) { if (!object[parts[i]]) { object[parts[i]] = {}; } object = object[parts[i]]; } return object; } }; YourGlobal.namespace('Books.MaintainableJavaScript'); YourGlobal.Books.MaintainableJavaScript.author = 'Nicholas C. Zakas'; YourGlobal.namespace('Books.HighPerformanceJavaScript'); console.log(YourGlobal.Books.MaintainableJavaScript.author); YourGlobal.namespace('Books').ANewBook = {}; </script> </body> </html>