Javascript nuances

Posted on Tuesday, November 20, 2007

Oh Javascript... you can't be intuitive can you?

I want to see if a function exists and then call it -

    if(decrementComments)
        decrementComments();

Wrong.

    if(typeof decrementComments == 'function')
        decrementComments();

Right.

Graffiti users of the world can have comments on their posts now. :)

Jayme

Comments

  • Ben Tiedt on 11.20.2007 at 2:55 PM
  • What's wrong with #1? I've used this hundreds of times. While its true that it only checks that the variable exists and is non-null (or zero or false), it should work for simple checks.

  • Jayme on 11.20.2007 at 3:17 PM
  • With #1, I get the following error if the function is not defined (IE and FF).

    decrementComments is not defined

    Line: if(decrementComments)

    If the function is there, it works fine.

    Jayme

  • Ben Tiedt on 11.20.2007 at 4:13 PM
  • Try if(window.decrementComments) instead -- when I've used this, it has always been against a member variable such as if(this._functionName).

  • steve on 6.15.2009 at 9:27 PM
  • thanks for shared

  • tutors on 7.30.2009 at 9:06 AM
  • Hi,

    thank for sharing the information. Say-it-with-a-one-click-live-demo is a great article...will bookmark it

  • No deposit poker on 7.31.2009 at 4:55 AM
  • I've used this hundreds of times. While its true that it only checks that the variable exists and is non-null (or zero or false), it should work for simple checks.

  • free games online on 7.31.2009 at 11:58 PM
  • Your article is much more informatics for all of the visitor. I am very happy to read it. This is really very nice. Thank you for it.

  • teeth whitener on 8.11.2009 at 2:31 AM
  • I tried every normal windows method and explorer, that I could think of as well as several supposedly great utility programs that I was told to try.

  • Villa Rentals on 8.11.2009 at 9:19 AM
  • What's wrong with my data. I could not get this script. I have tried many times. I try it few more times to get it.

  • free poker on 8.28.2009 at 6:07 AM
  • I could think of as well as several supposedly great utility programs that I was told to try.

  • Best colon cleanse on 10.08.2009 at 6:52 AM
  • thank for sharing the information. Say-it-with-a-one-click-live-demo is a great article...will bookmark it

Leave a Comment


Search this site

Last 3 Tweets

  • The Courier looks insanely cool... i am very impressed.
  • @hkarthik or a badass PC desktop. they still have a good purpose :)
  • @JustinEtheredge woah. that sounds like a real gem there... lol