【发布时间】:2010-03-10 08:57:05
【问题描述】:
我正在编写一个函数 add_event,如下所示:
function add_event(o, event_type, callback, capture){
o = typeof o === "string" ? document.getElementById(o) : o;
if(document.addEventListener){
add_event = function(o, event_type, callback, capture){
o = typeof o === "string" ? document.getElementById(o) : o;
o.addEventListener(event_type, callback, capture);
}
}else if(document.attachEvent){
add_event = function(o, event_type, callback, capture){
o = typeof o === "string" ? document.getElementById(o) : o;
o.attachEvent("on" + event_type, callback);
}
}
add_event(o, event_type, callback, capture);
}
现在我的问题是声明是否
o = typeof o === "string" ? document.getElementById(o) : o;
影响这个程序的性能?如果你传递一个 HTML 元素而不是一个 id,你实际上会执行语句 o = o,这就是我问这个问题的原因。非常感谢。
【问题讨论】:
标签: javascript performance variable-assignment