Link and data type checking are the same?

I have a simple function in my library that checks the correctness of the link to the object (the object here means a link to the created HTML element, mainly DIV). It looks like this:

function varIsValidRef(aRef) {
    return ( !(aRef == null || aRef == undefined) && typeof(aRef) == "object");
}

During the experiment, I found that this has the same effect:

function varIsValidRef(aRef) {
    return (aRef) && typeof(aRef) == "object";
}

I understand that there is some disagreement regarding the short hand () test? When testing for various data types (null, undefined, integer, float, string, array), I could not find the difference in the final result. The function works as expected.

Can you say that these two versions do the same?

+3
source share
4 answers

No, in my opinion, these functions do not work the same:


aRef undefined null, var - object, true.


aRef . null, undefined 0 false, true. true ( ), , .

, false, aRef - 0, . , , - .

, , . , , , (aRef) false:

varIsValidRef(0);
>>> 0

varIsValidRef('');
>>> ""

varIsValidRef(undefined);
>>> undefined

varIsValidref(null);
>>> null

JavaScript , , if - .

.

+1

:

!(aRef == null || aRef == undefined)

false "null", null, "undefined", undefined

(aRef)

0, "", false, null, undefined, NaN

+1

, .

, , , .

, aRef !false - (.. [], null, undefined) , , .

, , , - , .

, , , / , , , ( , ), , . . , , , , , .

0

, , , , . .

return ( !(aRef == null || aRef == undefined) && typeof(aRef) == "object");

nulland undefinedboth mean the state falsecombined with !infront, makes it equal to the expression aRef, which will return trueif both of them are either not null or are not undefined.

0
source

All Articles