Thursday, September 2, 2010

JSLints and evil twins == (again)

This time I have used JSLint to recheck and possibly
polish some of my older sourcecodes:

As expected JSLint gave me
several messages

Problem at line 3 character 5: Expected '===' and instead saw '=='.

I decided to turn it off with /*jslint eqeqeq: false*/
and surpise... only SOME of the messages disapeared.

Try this:

1./*jslint eqeqeq: true*/
2.if(a==undefined){}
3.if(a==null){}
4. if(a==b){}


result (expected):

Error:
Problem at line 2 character 5: Expected '===' and instead saw '=='.
if(a==undefined){}
Problem at line 3 character 5: Expected '===' and instead saw '=='.
if(a==null){}
Problem at line 4 character 5: Expected '===' and instead saw '=='.
if(a==b){}
Implied global: a 2,3,4, b 4

all 3 lines reported as error.

Now try to turn it off:

/*jslint eqeqeq: false*/
if(a==undefined){}
if(a==null){}
if(a==b){}

Only the last line is not reported any more, first two are still considered harmfull:

Error:
Problem at line 2 character 5: Use '===' to compare with 'undefined'.
if(a==undefined){}
Problem at line 3 character 5: Use '===' to compare with 'null'.
if(a==null){}
Implied global: a 2,3,4, b 4


Why do I care ?

From habit (maybe very wrong one)
I use construction:

function(arg1,arg2,arg3)
if(arg3 == null)
or
if(arg3 != null)

bacause IMHO this is valid:

null==null
true
undefined==null
true
null!=null
false
undefined!=null
false

To simplyfy ifing for both undefined and null values
Otherwise I would have to write:

if(arg3 === null || typeof arg3 !== "undefined")
or
if(arg3 === null || arg3 === undefined)

with second option I use known trick,
"elimination of evil global undefined"
BTW: also used by jQuery:
(function( window, undefined ) {

})(window);

Trying this

/*jslint eqeqeq: false*/
(function(undefined) {
if(a==undefined){}
}());

with JSLint you get:

Error:
Problem at line 2 character 11: Expected an identifier and instead saw 'undefined' (a reserved word).
(function(undefined) {
Problem at line 3 character 9: Use '===' to compare with 'undefined'.
if(a==undefined){}
Implied global: a 3

Solution ?

Can anyone tell me how to turn off JSLint ==
In the way it ignores all not only some constructions ?

Can anyone tell me how to make effective and simple if
which returns true only for "null and undefined" and false for all other values ?
Suggestion by Mr.D on his page
If you only care that a value is truthy or falsy, then use the short form. Instead of
(foo != 0)
just say
(foo)
is not and option because we are latking about specified or valid (0, false) and uspecified or invalid (null,undefined) here...

Or shell I rewrite all my ifs from if(a!=null) into strange looking:

/*jslint eqeqeq: false*/
(function(undef) {
if(a!=undef){}
}());



Thanx in advance....

Try also my favorite blogger at:
http://webreflection.blogspot.com/search?q=JSLint

Update: 2011/06/07

It seems that current version Edition 2011-07-01
http://www.jslint.com/
works fine for all 3 cases and works correctly for null and undefined as well:

1./*jslint eqeqeq: true*/
2.if(a==undefined){}
3.if(a==null){}
4. if(a==b){}

No comments:

Post a Comment