JavaScript Tips: Be care of "==" comparison
Author
Zhou Renjian
Create@
2005-12-18 23:47
function CSomeClass(){
/*
* A lot of functions here
*/
this.fun = function () {
//...
}
}
And you try to do "==" compare as follow:
var s = "Some";
var f = CSomeClass;
function check (x) {
if ("Some" == x) {
return "String: Some";
} else if (x == CSomeClass) {
return "Function: CSomeClass";
} else {
return "Other";
}
}
alert (check ("Some"));
alert (check (f));
The above codes should have no problem within Firefox/Mozilla and IE. But when:
for (var i = 0; i < 1000; i++) {
check (f);
}
Firefox/Mozilla will perform very bad but IE perform fast!
In Firefox/Mozilla, the "Some"==x comparison will make the JavaScript engine trying to parse the "x" into a string and then compare with "Some". But evaluate the "CSomeClass" into a string will be time-consuming, so it will in bad performance. Try to avoid such "==" comparison in such a way:
(typeof clazzTarget == "string" && "void" == clazzTarget)