关于javascript模块和命名空间(Module and namespace)
来源:互联网 发布:电子地图软件开发 编辑:程序博客网 时间:2024/05/23 12:54
我们做个项目想作为一个Module,要让它能在各个地方运行,就需要避免定义全局变量global variables和全局函数global function. 所以就要给自己的所有的variables和functions变成一个empty object 的属性或者方法,如例:
// Create an empty object as our namespace// This single global symbol will hold all of our other symbolsvar Module;if(Module && (typeof Module != "object" || Module.Name))throw new Error("Namespace 'Module' already exists");Module = {};// Define all variables and functions within the namespaceModule.max = 10;Module.define = function(){ /* code goes here*/}Module.provides = function(o, c){ /*code goes here*/}Module.sayHi = function(){ /* code goes here*/}
这样需要用的时候我们原来比如写 sayHi(), 现在这样写Module.sayHi();
另外现在更流行的一种是通过闭包作为私有命名空间和作用域,使用闭包来创建一个私有命名空间,然后将其共有方法导出到一个公有的命名空间中。
如例:
// Create the namespace object. Error checking omitted here brevity// Instead of using com.davidflanagan.Class// we can also simply say var Module={} as the example above// the reason to use com.davidflanagan.Class is just 100% ensure the name is UNIQUEvar com;if(!com) com = {};if(!com.davidflanagan) com.davidflanagan = {};com.davidflanagan.Class = {};// Don't stick anything into the namespace directly// Instead we define and invoke an anonymous function to create a closure// that serves as our private namespace. This function will export its// public symbols from the closure into the Module object// Note that we use an unnamed function so we don't create any other global symbols(function(){ // Begin anonymous function definition// Nested functions create symbols within the closurefunction define(data) {counter+=data; /* more code here*/}function provides(o, c) { /* code here*/}// local variable are symbols within the closure// This one will remain private within the closurevar counter = 0;function localMethod(){ return "This is local method"};console.log(localMethod()); // This is local method// This function can refer to the variable with a simple name// instead of having to qualify it with a namespacefunction getCounter(){ return counter;}// Now that we will defined the properties we want in our private closure// we can export the public ones to the public namespace// and leave the private ones hidden here.var ns = com.davidflanagan.Class;// Define all variables and functions within the namespacens.define = define;ns.provides = provides;ns.getCounter = getCounter;})(); // End anonymours function definition and invoke itcom.davidflanagan.Class.define(5);console.log(com.davidflanagan.Class.getCounter()); // 5console.log(com.davidflanagan.Class.localMethod()); // ERROR 命名空间中private function未经export不能被使用
更多详细内容见《犀牛书》第十章
- 关于javascript模块和命名空间(Module and namespace)
- 模块(Module)的用途1 命名空间(Namespace)
- 关于JavaScript的namespace命名空间
- 关于JavaScript的namespace命名空间
- 关于JavaScript的namespace命名空间
- 关于JavaScript的namespace命名空间
- javascript自定义namespace-命名空间
- JavaScript Namespace命名空间使用说明
- 关于C++命名空间namespace
- Javascript的命名空间namespace个人体会
- 关于Javascript模块化和命名空间管理
- 模块和命名空间
- 关于命名空间 using namespace std 解释
- 关于js中namespace命名空间模式
- 关于js中namespace命名空间模式
- PHP命名空间 namespace 和 自动加载
- 命名空间namespace的概念和使用
- 04 iostream和namespace命名空间
- Deb打包
- 计算文件MD5值 应用层篇
- 机房收费系统之组合查询的传参问题
- Android 悬浮拖到窗口例子
- 实战:监控sqlserver 2008 R2群集状态
- 关于javascript模块和命名空间(Module and namespace)
- 飞鸽出现传送文件失败?
- 更换新的博客!!!!!!!!!
- VisionMobile:从平台墓地得到的教训
- jap所依赖的jar包
- 在PV3D中获取鼠标点击位置的空间坐标信息
- break语句和continue语句的使用
- LAMP-3安装 GD
- getInitParameter方法解释