<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <meta charset="utf-8" /> </head> <body> </body> </html> <script> // 主要测试命名空间的注入测试 /** 1 使用场景是啥? 1.1 注重命名空间的处理, myapp是命名空间的名称, 1.2 utils是命名空间下模块的名称, 同一个命名空间下能够有多一个模块, 1.3 2 须要注意的要点 2.1 命名空间的屡次嵌套处理 3 关键性的内容, 在命名空间上使用apply函数, 类比构造函数继承的处理过程. **/ var myapp = myapp || {}; // 模块化. myapp.utils = {}; (function () { var value = 5; this.getValue = function () { return value; } this.setValue = function (newValue) { value = newValue; } this.tool = {}; }).apply(myapp.utils); //在命名空间内上添加属性, console.log(myapp); // (0) (function () { this.getName = function () { console.log("1"); } }).apply(myapp.utils.tool) // 深层次. console.log(myapp); //(1) /* console.log (0) object |-->utils:objecyt |-->getValue(); |-->setValue(); |-->tool:object; (1) object |-->utils:objecyt |-->getValue(); |-->setValue(); |-->tool:object |--> getName(); */ </script>