我正在寫一個函數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