ITeye博客

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

KeepCrazy · 2013-05-23 22:53 · ExtJs · 编程语言 · Markdown

在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;
	}

}
 

这样做的好处是可以集中精力于代码的核心内容, 提高阅读速度.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。