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

2048Re: [jslint] Re: has not been fully defined yet

Expand Messages
  • Satyam
    Mar 10 10:10 AM
    • 0 Attachment
      I would like to add that while undefined means "I don't have a clue what
      the value might be", null means "I know it has no value" (a known
      unknown :-P ). Beyond a certain point in the application, undefined
      might mean there is an error, null means I know it has no value.

      If more languages had null and undefined we wouldn't have silly
      conventions like indexOf returning -1 when an item was not found. In
      JavaScript it is an unfortunate legacy of other languages. Ideally, you
      could have:

      var position = 'abc'.indexOf('a') sets position to 0
      var position = 'abc'.indexOf('x') should set position to null, not -1
      var position = null.indexOf('a') should leave position undefined
      (assuming the error was not caught)

      All those (0, null and undefined) are falsy but they are far from
      meaning the same. The last one shows why I said that a variable set to
      undefined might signal an error when found beyond a certain point.

      In the second case, returning false would also be an arbitrary
      convention, just as -1 is. It might be agreed that false means 'it is
      not there' but indexOf asks for the position of the item sought, not if
      it is there (where false would be a logical reply) and if it is not
      there, it has no position, no such thing, thus null, not false. Anyway,
      this is beyond the point.

      Satyam


      El 10/03/2011 17:53, abyssoft@... escribió:
      >
      > In response to the last question,
      >
      > **********
      > --- In jslint_com@yahoogroups.com
      > <mailto:jslint_com%40yahoogroups.com>, John Hawkinson <jhawk@...> wrote:
      > > Actually, you proposed:
      > >
      > > var foo = null;
      > > foo = bar(function() { if (foo) { foo.m(); } }); // Case 5
      > >
      > > but why bother to initialize foo to null? The truthiness of null and
      > > and undefined is the same...
      > **********
      >
      > The Truthiness of null and undefined are not the same.
      > while null == undefined is true, null === undefined is false; if the
      > Truthiness was the same then null === undefined would be true.
      > Also the intent of null and undefined are different in nature. When a
      > variable is declared it is by definition is undefined. Which is to say
      > it is not empty, but rather that it has yet to be defined. Null is a
      > empty state, think of Null in terms of math with help with this
      > concept as null is also known as the empty set. Undefined in math
      > terms and null (empty) in math terms are not equivalent (undefined !==
      > null) but they are similar (undefined == null).
      >
      > Declare but leave uninitialized
      >
      > var someVariable1; // assignment to undefined is understood
      > var someVariable2 = undefined; // does the same as example 1 but will
      > throw lint warning, use example 1
      >
      > someVariable1 = 123; // assign value to variable
      > someVariable1 = undefined; // destruct variable but leave declared
      >
      > Declare and initialize to empty
      > var someVariable3 = null;
      > someVariable1 = 123; // assign value to variable
      > someVariable1 = null; // empty variable
      >
      > Hope this short post clears up confusion between undefined and null.
      >
      >
      > ------------------------------------------------------------------------
      >
      > No virus found in this message.
      > Checked by AVG - www.avg.com <http://www.avg.com>
      > Version: 10.0.1204 / Virus Database: 1497/3495 - Release Date: 03/09/11
      >


      [Non-text portions of this message have been removed]
    • Show all 24 messages in this topic