Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jslint error: Unexpected 'in'. Compare with undefined, or use the hasOwnProperty

Tags:

I have the following javascript function that fails a jslint check

  function hasActiveX() {
    return ('ActiveXObject' in window);
  }

jslint error

Unexpected 'in'. Compare with undefined, or use the hasOwnProperty method instead.

should I just live with the jslint error, or is there a better way to determine ActiveXObject? I could not find a jslint flag to skip this check?

like image 909
house9 Avatar asked Jul 26 '11 03:07

house9


2 Answers

Ignore the error. The "in" operator is clearly defined in ECMA 262-5.1 / June 2011 sec-11.8.7

There appear to be only three outcomes for 'someProperty' in anObject which is one of: true, false, or a TypeError exception is thrown. When it evaluates to true, then there is definitely 'someProperty' in anObject. When it evaluates to false there is definitely not 'someProperty' in anObject. When a TypeError is thrown there is definitely not 'someProperty' in anObject because anObject is either null or it isn't an object at all. This all seems very clear to me. When I want to know if an object has a property and, I don't care if that property is the object's own property or being inherited and, I don't care what the value of the property is, then I simply look for 'someProperty' in anObject.

Warning

Caution: some would have you check for anObject.someProperty !== undefined but that isn't really checking whether or not the object has the property. What it's doing is checking whether the object has the property AND that the value of that property is NOT undefined. Some would have you check for anObject.hasOwnProperty('someProperty'); but that will only tell you if the object has that property AND has NOT inherited it somehow. Don't believe me? Try the following:

console.log(document.body.tagName);
// BODY

console.log(document.body.hasOwnProperty('tagName'));
// false, it's inherited

console.log('tagName' in document.body);
// true, it does have the property

document.body.wobbles = undefined;
// new property added to document.body

console.log('wobbles' in document.body);
// true, it does have the property

console.log(document.body.wobbles !== undefined);
// false, the value really IS undefined

I've written an article about the "in" operator that goes into more detail. If you want to read it it's at: http://matthewkastor.blogspot.com/2012/09/Unexpected--in---Compare-with-undefined--or-use-the-hasOwnProperty-method-instead.html The bottom line is that you should just ignore this error and wrap things up in a try catch block if the object might be null or not an object.

function hasProperty(prop, obj) {
    try {
        return prop in obj;
    } catch(e) {
        return e;
    }
}
like image 77
Kastor Avatar answered Sep 21 '22 12:09

Kastor


I think JSLint is asking you to try:

function hasActiveX() {
    return window.hasOwnProperty('ActiveXObject');
}

Or the other suggestion of comparing against "undefined":

return (typeof(window.ActiveXObject) != "undefined");

Personally, I prefer the former.

After reading the comments, it seems like in is actually useful if JSLint would stop complaining about it, otherwise I would try option 2 above.

like image 24
Cᴏʀʏ Avatar answered Sep 24 '22 12:09

Cᴏʀʏ