On Fri, Apr 15, 2011 at 5:08 AM, J.R. <[email protected]> wrote:
> 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.

In this case both sides of the conditional expression contains strings
and no type conversion is necessary so "!=" is preferred.

Strict comparison "!==" could be used without any problem, but there
is no real reason to use it in this case once we know the operation we
want to perform and the results we expect.

You could also use ".test()" to see if strings are equals, that would
still give the same result but there is no reason accompanying the
choice. ;-)

>>
>> 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.
>

Yes, the "in" operator may be microseconds slower than the standard
value accessors but those checks should only be executed once at
initialization so speed should not be the metering for the choice
here.

You are not interested in the values you read anyway, just their
existence, so the "in" operator also seems more semantic in the
context it is used.

--
Diego


>>
>> `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]
>

-- 
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]

Reply via email to