`
KeepCrazy
  • 浏览: 59424 次
社区版块
存档分类
最新评论

阅读源代码时可进行适当简化

 
阅读更多
[size=medium]
在Ext的源代码中含有很多用于数据验证或消除浏览器差异的、与功能逻辑没有关系的代码。这类代是保证Ext项目正常运行所必需的, 但它们的存在却给源代码的阅读带来很多干扰, 大大加大了阅读难度。 在初读源码后, 我常常会把代码进行简化:只留下部分验证(可以理解的那一部分)和所必需的逻辑处理代码, 为消除浏览器而存在的代码基本被省略。

getDom : function(el, strict){
            if(!el || !DOC){
                return null;
            }
            if (el.dom){
                return el.dom;//不是很明白
            } else {
                if (typeof el == 'string') {
                    var e = DOC.getElementById(el);
                    // IE returns elements with the 'name' and 'id' attribute.
                    // we do a strict check to return the element with only the id attribute
                    if (e && isIE && strict) { //用于消除浏览器差异的, 不理解 
                        if (el == e.getAttribute('id')) {
                            return e;
                        } else {
                            return null;
                        }
                    }
                    return e;
                } else {
                    return el;
                }
            }
        }
 


被我简化为:
getDom: function (el){
	if(!el){
		return null;
	}

	if(el.dom){
		return el.dom;
	}

	if(typeof el=="string"){
		var e=document.getElementById(el);
		return e;
	}else{
		return el;
	}

}
 

这样做的好处是可以集中精力于代码的核心内容, 提高阅读速度.[/size]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics