Does undefined check ever throw an error in JavaScript without typeof in IE6?
I don't have an access to IE6 right now, so, I am asking here.
If I have a comparison such as:
// imagine App being declared as an obj somewhere...
if (App.errorLog === undefined) {
App.errorLog = [];
}
Would that code throw an error in IE6 if the property was never declared or defined anywhere? It seems to work fine in other browsers even in IE7. I just think that I had some problems with IE6 while a back and used typeof
to solve those problems, but I 开发者_C百科am not sure.
Properties that were not set will simply return the value of undefined
. Only undeclared variables will actually raise a ReferenceError
, this is where you then have to use typeof foo === 'undefined'
.
So yes, your code will work.
works fine in IE6 as you have it
The typeof
trick resolves issues where the global undefined
has been overwritten, or where a variable has not been declared.
In your case, you're just checking for the existence of a property in the App
object. As long as App
exists, there shouldn't be a declaration issue.
If there's a chance that some code will overwrite undefined
, then you need typeof
.
Another way to test for a property is:
if ( !('errorLog' in App) ) {
App.errorLog = [];
}
This will also look at members of the prototype
, and so is equivalent to checking for undefined
and it gets around the issue of undefined
being overwritten.
i believe it would not throw an exception but simply it would go out of the if condition. also i have not heard of undefined as value. if you are trying to say that if the App.errorlog is not set then the code should be App.errorlog == null.
精彩评论