Loading ...
Sorry, an error occurred while loading the content.

Re: [jslint] option.eqeq

Expand Messages
  • livefree75
    ... You re right of course on the compare_types vs. strict - I just typed that up quickly in reply to the message, and changed the variable name in the
    Message 1 of 13 , Jun 17, 2011
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
      >
      > --- In jslint_com@yahoogroups.com, "livefree75" <jpittman2@> wrote:
      > >
      > > I have found only one instance in all my code where == is useful.
      > >
      > > There are times when I want to see if a given value is in an array, but
      > > I don't care (or want to worry about) what its type is... e.g. the value
      > > of a Select object, which is a defined as a string, being compared
      > > against an array of integers.
      > >
      > > I created a function that checks whether a given value is in an array,
      > > and you can specify whether to care about type. Something like this:
      > >
      > > /**
      > > * Checks the existence of an item in an array.
      > > * @param Array arr The array to search
      > > * @param mixed item The item to search for
      > > * @param Boolean strict If false, does not compare types
      > > * (default is true.)
      > > */
      > > function inArray(arr, item, strict) {
      > > var i=0;
      > > if (compare_types !== false) {
      > > for (; i<arr.length; i++) {
      > > if (arr[i] === item) {
      > > return true;
      > > }
      > > }
      > > } else { // strict === false
      > > for (; i<arr.length; i++) {
      > > if (arr[i] == item) {
      > > return true;
      > > }
      > > }
      > > }
      > > return false;
      > > }
      > >
      > > This fails JSLint because of the == in the second for loop. However,
      > > checking for Object equality, then doing .toString() seems like much
      > > more work than just using ==.
      >
      >
      > I found zero useful instances of == in your code. Your use of == resulted in two inefficiencies and one error. The first if is looking at compare_types, but it probably should be looking at strict instead.
      >
      > I think the use of strict should be
      >
      > if (!strict) {
      > item = +item;
      > }

      You're right of course on the compare_types vs. strict - I just typed that up quickly in reply to the message, and changed the variable name in the definition but forgot to do it in the function body.

      item = +item would coerce item into a number, so my function would be useless for strings, as all non-numeric strings would be coerced into NaN.

      Am I missing something?

      >
      > That way JS does not have to do type coercion on every iteration.
      > Even better, you do not need the extra == loop.
      >
      > A further improvement would be to use array.indexOf instead.

      OK - now that I'm looking at my actual code - I wasn't doing it for array - I was doing it for Objects, and it's in the context of jQuery:

      (function($) {
      $.extend({
      inObject : function(value, object, strict) {
      var found_key = null;
      if (strict !== false) {
      $.each(object, function(key, val) {
      if (val===value) {
      found_key = key;
      return false;
      }
      });
      } else {
      $.each(object, function(key, val) {
      if (val==value) {
      found_key = key;
      return false;
      }
      });
      }
      return found_key;
      } // inObject()
      });
      }(jQuery));


      >
      > JSLint's advice was good. You should have taken advantage of it.
      >

      Well that's the point of forums like this, right?

      BTW - your forin directive doesn't flag $.each() constructs.

      Also, before I used $.each(), I used to do this (this was also before I knew about the Object.hasOwnProperty()):

      for ( i in obj ) {
      if (typeof obj[i] !== 'function') {
      // body of my for-in loop
      }
      }

      But I would want to do this, to avoid the busywork of indenting.
      for ( i in obj ) {
      if (typeof obj[i] === 'function') { continue; }
      // body of my for-in loop
      }

      But JSLint didn't accept that as proper filtering, which of course it is. I would assume the same is true for Object.hasOwnProperty().

      Jamie
    Your message has been successfully submitted and would be delivered to recipients shortly.