[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

What's wrong with this code?

denissavenok

2/26/2015 9:18:00 AM

The function doesn't work as expected. Why?

var isUndefinedOrNull = function(obj) {
return /^(?:undefined|null)$/.test(typeof obj);
};
7 Answers

ram

2/26/2015 11:05:00 AM

0

denissavenok@gmail.com writes:
>var isUndefinedOrNull = function(obj) {
> return /^(?:undefined|null)$/.test(typeof obj);
>};

// This stands since the beginning of JavaScript
typeof null === 'object';

Thomas 'PointedEars' Lahn

2/26/2015 11:17:00 AM

0

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.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

Ben Bacarisse

2/26/2015 2:25:00 PM

0

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.

Thomas 'PointedEars' Lahn

2/26/2015 3:04:00 PM

0

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

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

Ben Bacarisse

2/26/2015 3:38:00 PM

0

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.

Thomas 'PointedEars' Lahn

2/26/2015 4:09:00 PM

0

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?...
<http://PointedEars.de/wsvn/JSX/trunk/obj...

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

JR

2/26/2015 5:10:00 PM

0

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

and

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


--
Joao Rodrigues