Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26933 > unrolled thread

What's wrong with this code?

Started bydenissavenok@gmail.com
First post2015-02-26 01:18 -0800
Last post2015-02-26 14:09 -0300
Articles 7 — 4 participants

Back to article view | Back to comp.lang.javascript


Contents

  What's wrong with this code? denissavenok@gmail.com - 2015-02-26 01:18 -0800
    Re: What's wrong with this code? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-26 12:17 +0100
    Re: What's wrong with this code? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-02-26 14:25 +0000
      Re: What's wrong with this code? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-26 16:04 +0100
        Re: What's wrong with this code? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-02-26 15:37 +0000
          Re: What's wrong with this code? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-26 17:09 +0100
    Re: What's wrong with this code? Joao Rodrigues <groups_jr-1@yahoo.com.br> - 2015-02-26 14:09 -0300

#26933 — What's wrong with this code?

Fromdenissavenok@gmail.com
Date2015-02-26 01:18 -0800
SubjectWhat's wrong with this code?
Message-ID<b2751271-cbda-4bcd-9217-a37eb3fa58b0@googlegroups.com>
The function doesn't work as expected. Why?
    
var isUndefinedOrNull = function(obj) {
        return /^(?:undefined|null)$/.test(typeof obj);
};

[toc] | [next] | [standalone]


#26936

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-02-26 12:17 +0100
Message-ID<1462385.MiWHP334DG@PointedEars.de>
In reply to#26933
Stefan Ram wrote:

> denissavenok@gmail.com writes:
>> var isUndefinedOrNull = function(obj) {
>>        return /^(?:undefined|null)$/.test(typeof obj);
>> };
> 
> // This stands since the beginning of JavaScript
> typeof null === 'object';

Except that in the first JavaScript/JScript versions there was no “===” 
operator :)  But using “==” instead does not change the semantics of this 
expression as the operands have the same type (String).

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26937

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-02-26 14:25 +0000
Message-ID<8761aopx65.fsf@bsb.me.uk>
In reply to#26933
denissavenok@gmail.com writes:

> The function doesn't work as expected. Why?
>     
> var isUndefinedOrNull = function(obj) {
>         return /^(?:undefined|null)$/.test(typeof obj);
> };

If the type of obj is Null, typeof is defined to yield 'object'.

You might (in some very odd implementation) get 'null' from typeof, but
that's only because the result is up to the implementation in some very
specific cases.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#26938

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-02-26 16:04 +0100
Message-ID<2083952.afr504CXY5@PointedEars.de>
In reply to#26937
Ben Bacarisse wrote:

> denissavenok@gmail.com writes:
>> The function doesn't work as expected. Why?
>>     
>> var isUndefinedOrNull = function(obj) {
>>         return /^(?:undefined|null)$/.test(typeof obj);
>> };
> 
> If the type of obj is Null, typeof is defined to yield 'object'.
> 
> You might (in some very odd implementation) get 'null' from typeof, but
> that's only because the result is up to the implementation in some very
> specific cases.

To be exact, a conforming implementation of ECMAScript could only provide  
"null" as the result of a “typeof” operation if the operand was a reference 
to a host object.  An implementation that provided "null" for a primitive 
value or a reference to a native object would be non-conforming, which in 
*this* case means it is borken.

<http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,
%201st%20edition,%20June%201997.pdf#page=50>
<http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,
%202nd%20edition,%20August%201998.pdf#page=55>
<http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,
%203rd%20edition,%20December%201999.pdf#page=59>
<http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262%205th%20edition%20December%202009.pdf#page=81>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26939

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2015-02-26 15:37 +0000
Message-ID<87mw40n0or.fsf@bsb.me.uk>
In reply to#26938
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:

> Ben Bacarisse wrote:
>
>> denissavenok@gmail.com writes:
>>> The function doesn't work as expected. Why?
>>>     
>>> var isUndefinedOrNull = function(obj) {
>>>         return /^(?:undefined|null)$/.test(typeof obj);
>>> };
>> 
>> If the type of obj is Null, typeof is defined to yield 'object'.
>> 
>> You might (in some very odd implementation) get 'null' from typeof, but
>> that's only because the result is up to the implementation in some very
>> specific cases.
>
> To be exact, a conforming implementation of ECMAScript could only provide  
> "null" as the result of a “typeof” operation if the operand was a reference 
> to a host object.

To be more exact, only if the reference is to a host object that does
not implement [[Call]].  For references to callable object, both native
and host, the result must be 'function'.

<snip>
-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#26940

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-02-26 17:09 +0100
Message-ID<2865092.U4gHO1vGHG@PointedEars.de>
In reply to#26939
Ben Bacarisse wrote:

> Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
>> Ben Bacarisse wrote:
>>> You might (in some very odd implementation) get 'null' from typeof, but
>>> that's only because the result is up to the implementation in some very
>>> specific cases.
>> To be exact, a conforming implementation of ECMAScript could only provide
>> "null" as the result of a “typeof” operation if the operand was a
>> reference to a host object.
> 
> To be more exact, only if the reference is to a host object that does
> not implement [[Call]].  For references to callable object, both native
> and host, the result must be 'function'.

True.  So much for theory.  In practice, the result is "object" for callable 
host objects in the MSHTML DOM until MSHTML 8.0.x inclusive.

<http://PointedEars.de/wsvn/JSX/trunk/types.js?rev=15>
<http://PointedEars.de/wsvn/JSX/trunk/object.js>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26941

FromJoao Rodrigues <groups_jr-1@yahoo.com.br>
Date2015-02-26 14:09 -0300
Message-ID<mcnk0c$i59$1@speranza.aioe.org>
In reply to#26933
On 02/26/2015 06:18 AM, denissavenok@gmail.com wrote:
> The function doesn't work as expected. Why?
>
> var isUndefinedOrNull = function(obj) {
>          return /^(?:undefined|null)$/.test(typeof obj);
> };
>

As (x === undefined || x === null) is the same as (x == null), we may 
rewrite the function to:

var isUndefinedOrNull = function (obj) {
   return (obj == null);
};

See:

<http://davidwalsh.name/fixing-coercion>

and

<http://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/>


-- 
Joao Rodrigues

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web