开发者

Testing for validity

开发者 https://www.devze.com 2022-12-31 04:59 出处:网络
I\'d like to know the difference (if any) between the following: if( someDOMElement.someProperty ) { ...

I'd like to know the difference (if any) between the following:

if( someDOMElement.someProperty )
{
...

if( someDOMElement.someProperty != null )
{
...

if( s开发者_运维问答omeDOMElement.someProperty != undefined )
{
...

Is one safer than the others?


Those will all do the same thing, and one isn't more error-prone than the others. Whereas if you were using !== rather than !=, the second two would only be true if the value really were null (the second one) or undefined (the third one), because the !== operator doesn't do coercion.

Javascript will coerce values during comparisons with != or ==, so for example:

alert(false == 0);   // alerts "true"
alert(false === 0);  // alerts "false"

The === and !== operators let you control that behavior. The rules for what coercions occur are detailed in the spec (and a bit complicated), but just a simple "is this thing not 0, "", null, or undefined?" can be written simply if (thingy) and it works well. 0, "", null, and undefined are all "falsey".

Sean Kinsey has a point about some host objects, though I think most if not all DOM element properties will be fine. In particular, I've seen COM objects exhibit some interesting behavior, such as if (comObject.property) evaluating true when if (comObject.property == null) also evaluates true. (In my case, it was COM objects exposed as part of the server-side API of a product I was using; I use Javascript server-side as well as client-side.) Worth being aware that that can happen. When you're dealing with Javascript objects and (in my experience) DOM objects, you're fine.


Assuming that someDOMElement is not null, no particular differences:

http://www.steinbit.org/words/programming/comparison-in-javascript

There would be a difference if you use !==


Depending on exactly what someDOMElement is then these can have very different results, and none of them are 'safe' to use if Host Objects are involved (objects implemented as ActiveXObjects for instance).

You should really use a method such as one of these

// use this if you expect a callable property
function isHostMethod(object, property){
    var t = typeof object[property];
    return t == 'function' ||
    (!!(t == 'object' && object[property])) ||
    t == 'unknown';
}

// use this if you are only looking for a property
function isHostObject(object, property){
    return !!(typeof(object[property]) == 'object' && object[property]);
}

alert(isHostObject(someDOMElement, "someProperty")) 

You can read more about proper feature detection at http://peter.michaux.ca/articles/feature-detection-state-of-the-art-browser-scripting


depends on what you call a value.

if(someDOMElement && someDOMElement.someProperty !=undefined){
    the property exists and has been set to a value other than undefined or null-
    it may be 0, false, NaN or the empty string, as well as any truthy value
}
0

精彩评论

暂无评论...
验证码 换一张
取 消