惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

腾讯CDC
Hacker News: Ask HN
Hacker News: Ask HN
S
Securelist
Security Latest
Security Latest
S
Schneier on Security
T
Threat Research - Cisco Blogs
Latest news
Latest news
Cyberwarzone
Cyberwarzone
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
T
The Exploit Database - CXSecurity.com
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
T
Tor Project blog
Blog — PlanetScale
Blog — PlanetScale
C
Cyber Attacks, Cyber Crime and Cyber Security
C
CERT Recently Published Vulnerability Notes
The Hacker News
The Hacker News
月光博客
月光博客
WordPress大学
WordPress大学
博客园 - 叶小钗
Hugging Face - Blog
Hugging Face - Blog
美团技术团队
量子位
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cisco Blogs
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
Project Zero
Project Zero
Webroot Blog
Webroot Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
L
LINUX DO - 最新话题
Schneier on Security
Schneier on Security
Engineering at Meta
Engineering at Meta
www.infosecurity-magazine.com
www.infosecurity-magazine.com
aimingoo的专栏
aimingoo的专栏
D
Docker
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
宝玉的分享
宝玉的分享
T
Troy Hunt's Blog
L
Lohrmann on Cybersecurity
T
The Blog of Author Tim Ferriss
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
L
LangChain Blog

博客园 - fengyv

塑造职场影响力的五大法宝 怎样培养独挡一面的能力 数据结构 - 归并排序(merging sort) [分享]恼人的设计模式 Git使用总结 设计师整理的系统开发流程-简洁又有重点 JavaScript中的String对象 python高效解析日志入库 HTML CSS——margin和padding的学习 三层浅析及示例分析 C语言的代码内存布局详解 超级立方体小记 如何和项目经理沟通产品的交付? CentOS配置smaba与Windows共享文件 Javascript实现简单的下拉二级菜单 从测试员到测试负责人 项目团队中4种组员类型的相应管理方式 在软件项目管理中如何把时间估算的靠近真实值? 性能优化——算法优化
如何让js不产生冲突,避免全局变量的泛滥,合理运用命名空间
fengyv · 2014-06-15 · via 博客园 - fengyv

为了避免变量之间的覆盖与冲突,可以生成命名空间,命名空间是一种特殊的前缀,在js中,通过{ }对象实现。

在不同的匿名函数中,根据功能声明一个不同的命名空间,每个匿名函数中GLOBAL对象的属性都不直接挂在GLOBAL上,而是挂在次匿名函数的命名空间下,如:

  1. <script type="text/javascript">  
  2.    var GLOBAL={}  
  3. </script>  
  4. <script type="text/javascript">  
  5.    (function(){  
  6.       var a=123a1=256;  
  7.       GLOBAL.A={}  
  8.       GLOBAL.A.str=a;  
  9.    })();  
  10. </script>  
  11.   
  12. <script type="text/javascript">  
  13.    (function(){  
  14.       var b1=123b2=256;  
  15.       GLOBAL.B={}  
  16.       GLOBAL.B.str=a;  
  17.    })();  
  18. </script>  

如果同一个匿名函数中的程序非常复杂,变量名很多,命名空间可以进一步扩展,生成二级命名空间:

  1. <script type="text/javascript">  
  2.     var GLOBAL={}  
  3. </script>  
  4. <script type="text/javascript">  
  5.    (function(){  
  6.       var a=123a1=256;  
  7.       GLOBAL.A={};  
  8.       GLOBAL.A.CAT={};  
  9.       GLOBAL.A.DOG={};  
  10.       GLOBAL.A.CAT.name="mini";  
  11.       GLOBAL.A.CAT.move=function(){  
  12.       }  
  13.       GLOBAL.A.DOG.name="mini";  
  14.       GLOBAL.A.DOG.move=function(){  
  15.       }  
  16.    })();  
  17. </script>  


因生成命名空间是非常常用的功能,可以进一步将生成命名空间的功能定义成一个函数,方便调用,如下:

  1. <script type="text/javascript">  
  2.    var GLOBAL={}  
  3.    GLOBAL.namespace=function(str){  
  4.       var arr=str.split("."), o=GLOBAL;  
  5.             for(i=arr[0]=="GLOBAL"?1:0;i<arr.length; i++){  
  6.       o[arr[i]]=o[arr[i]] || {};  
  7.       o=o[arr[i]];  
  8.       }  
  9.    }  
  10. </script>  

调用命名空间具体操作:

  1. <script type="text/javascript">  
  2. //=============================================================  
  3. //     功能A  
  4. //     工程师甲  
  5. //     email:ctkl68945@gmail.com     msn:ctkl68945@hotmail.com"  
  6. //     2012-11-06  
  7. //=============================================================  
  8.    
  9. (function(){  
  10.    var a=123a1="hello world";  
  11.    GLOBAL.namespace("A.CAT");  
  12.    GLOBAL.namespace("A.DOG");  
  13.    GLOBAL.A.CAT.name="mini";  
  14.    GLOBAL.A.CAT.move=function(){  
  15.    }  
  16.    GLOBAL.A.DOG.name="mini";  
  17.    GLOBAL.A.DOG.move=function(){  
  18.    }  
  19.    GLOBAL.A.str=a;  
  20.    GLOBAL.A.str1=a1;  
  21. })();  

依次类似,无论多人的直接团队开发,还是个人的间接团队合作,都需要良好的可维护性。

1、添加必要的代码注释

2、让JS不产生冲突,需避免全局变量的泛滥,合理使用命名空间