我正在写一个函数add_event如下面所示:此javascript语句是否影响性能?
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,这就是我问这个问题的原因。万分感谢。
一个字符串并不一定是'String'的实例(不是'string'!)。 “typeof”是正确的选择。 – Gumbo 2010-03-10 09:10:20
@Gumbo变了。 – Amarghosh 2010-03-10 09:22:38