高品质的JavaScript

整理书籍内容(QQ:283125476 发布者:M 【重在分享,有建议请联系-》QQ号】)

养成良好的编程习惯

##如何避免团队JS冲突
* 避免实用全局变量【可使用匿名函数进行处理】以避免全局变量引起的冲突;
* 给程序一个统一的接口;

可复用性

  • 将id转换为class
  • 给元素添加跟节点挂钩 J_tab

<a id="a" href="http://www.adanghome.com" blogName="阿当的博客" blogType="前端开发">my blog</a>

var node = document.getElementById("a");     alert(node.blogName); 
 IE : 阿当的博客
 Firefox : undefined  
 alert(node.blogType); 
 IE : 前端开发
 Firefox : undefined 
 alert(node.getAttribute("blogName")); 
 IE 和Firefox : 阿当的博客
 alert(node.getAttribute("blogType")); 
 IE 和Firefox : 前端开发
<a id="a" href="http://www.adanghome.com" blogInfo="{name:'阿当的博客',type:'前端开
发'}">my blog</a> 
<script type="text/JavaScript"> 
 var node = document.getElementById("a"); 
 var info = node.getAttribute("blogInfo"); 
 alert(typeof info);   // string 
alert(info.name);   // undefined 
alert(info.type);   // undefined 
 info = eval("("+info+")"); 
 alert(typeof info);   // object 
alert(info.name);   // 阿当的博客
alert(info.type);   // 前端开发
</script> 
通过分享,结交好友~ 如本文对您有益,请给予关注。转载请注明出处!-- 小数
原文地址:https://www.cnblogs.com/mcat/p/4506432.html