On Wednesday, 13 April 2011 19:37:59 UTC-3, Asen Bozhilov wrote:
>
> J.R.:
>
> > Instead of:
> > if (window.ActiveXObject) {
> >
> > We should use:
> > if (typeof window.ActiveXObject !== "undefined") {
>
> I would not use both approaches. I would use:
>
> if (typeof ActiveXObject != 'undefined') {
> //...
> }
>
As the typeof operator (ECMA-262 3rd ed, 11.4.3) returns a string, then it
is safe, in this case, to use either the != or !== equality operators.
> Or as Diego proposed:
> if ('ActiveXObject' in this) {
> //...
> }
>
> When this code is evaluated in Global Execution Context everything
> will be OK and the code is portable in every environment which use
> JScript implementation.
>
Okay, but the in operator (ECMA-262 3rd ed, 11.8.7) seems to be slower than
the typeof operator and also can throw a TypeError exception... Therefore, I
think it is safer to use the typeof operator in this case.
> `ActiveXObject' is a built-in function in JScript. It is a Microsoft
> extension of JScript. For that reason I would use typeof with
> `ActiveXObject' identifier which resolved from Scope Chain or directly
> check the prototype chain of Global Object for property name
> `ActiveXObject`.
>
> I do not know how that test is reasonable when we need certain
> instance of ActiveXObject. The problem here is in the architecture of
> `ActiveXObject'. For example to access the file system, you should:
>
> var fso = new ActiveXObject("Scripting.FileSystemObject");
>
> You cannot be sure that will not throw an error. So the better
> approach is to wrap in try-catch block.
>
> var fso;
> try {
> fso = new ActiveXObject("Scripting.FileSystemObject");
> }catch (e) {}
>
> And you can skip the part where detect existence of `ActiveXObject' in
> the scope chain. For example XHR:
>
> if (window.XMLHttpRequest != undefined) {
> //...
>
Sorry, but haven't you meant:
if (typeof XMLHttpRequest != 'undefined') {... ????
Cheers,
Joao Rodrigues (J.R.)
>
--
To view archived discussions from the original JSMentors Mailman list:
http://www.mail-archive.com/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]