Duplicate funtion names - what happens?

Discussion in 'Javascript' started by Randell D., Nov 3, 2003.

  1. Randell D.

    Randell D. Guest

    Folks,

    I'm a newbie when it comes to JavaScript and whenever I visit a website
    curiosity sometimes get's the better of me and I'll inspect some of their
    code - It teaches me new things (either methods, or alternative solutions to
    a problem). I'm not talking about pinching someone else's code - so don't
    flame me.

    I notice though that while taking a look at the source view of
    http://news.independent.co.uk/uk/crime/ that there seemed to be some
    javascript functions that were delcared twice - is this just bad programming
    practice or is there something that I don't know about... I can program
    reasonably well in PHP and a handful of other languages and I would have
    expected either an error (that a function had already been delcared) or else
    the second function would take precedance over the first.... An exerpt of
    the source code follows to back up my example... I'm just wondering though
    the reason - bad programmer or what?

    I'd welcome comments...

    Cheers
    randelld

    (*some* of the code is included below - note two functions called
    storeCookie... why?)
    <SCRIPT TYPE="text/javascript"><!--

    function storeCookie(key,val) {
    var age = 100*24*3600 * 1000; // ms in 100 days
    storeCookieKey( key, val, age, true );
    }//storeCookie


    function storeCookie(key,val,age,reloadPg) {
    var expDate = new Date();
    var expTime = expDate.getTime() + age;
    expDate.setTime( expTime );
    document.cookie = key +"="+ val + "; expires="+ expDate.toGMTString() ;


    if( reloadPg )
    window.location.reload();
    }//storeCookie
    Randell D., Nov 3, 2003
    #1
    1. Advertising

  2. "Randell D." <> wrote in message
    news:kwhpb.263294$pl3.58800@pd7tw3no...
    <snip>
    >function storeCookie(key,val) {
    > var age = 100*24*3600 * 1000; // ms in 100 days
    > storeCookieKey( key, val, age, true );
    >}//storeCookie
    >
    >
    >function storeCookie(key,val,age,reloadPg) {
    > var expDate = new Date();
    > var expTime = expDate.getTime() + age;
    > expDate.setTime( expTime );
    > document.cookie = key +"="+ val + "; expires="+

    expDate.toGMTString() ;
    > if( reloadPg )
    > window.location.reload();
    >}//storeCookie

    <snip>

    It looks like a cut-and-paste mistake by the page author. From the
    parameter lists I would think that the second function was supposed to
    be called "storeCookieKey" but the author copied the first function,
    changed the body and the parameter list, but forgot to change the name
    as well. The error is not apparent because the second function
    definition replaces the first and the resulting storeCokie function is
    error free when called with the parameters intended for the first. On
    the other hand the function does not appear to ever be called by code on
    that page so neither should really be there. But then, there is so much
    JavaScript on that page that probably the whole lot should have been
    moved into an external JS file to cut down on the download, both by
    having it cached client side and for the users of non-JavaScript
    browsers who would not need it at all.

    Incidentally, as you admit to not knowing much about JavaScript, may I
    recommend that you not promote <URL: http://javascript.internet.com >
    as, while they may make thousands of simple scripts available, there
    does not appear to be any informed vetting of those scripts and as far
    as I could tell none are good and most are extremely poor and not even
    close to a suitable standard for Internet use.

    Richard.
    Richard Cornford, Nov 3, 2003
    #2
    1. Advertising

  3. Randell D.

    Randell D. Guest

    "Richard Cornford" <> wrote in message
    news:bo4ebr$cqa$1$...
    > "Randell D." <> wrote in message
    > news:kwhpb.263294$pl3.58800@pd7tw3no...
    > <snip>
    > >function storeCookie(key,val) {
    > > var age = 100*24*3600 * 1000; // ms in 100 days
    > > storeCookieKey( key, val, age, true );
    > >}//storeCookie
    > >
    > >
    > >function storeCookie(key,val,age,reloadPg) {
    > > var expDate = new Date();
    > > var expTime = expDate.getTime() + age;
    > > expDate.setTime( expTime );
    > > document.cookie = key +"="+ val + "; expires="+

    > expDate.toGMTString() ;
    > > if( reloadPg )
    > > window.location.reload();
    > >}//storeCookie

    > <snip>
    >
    > It looks like a cut-and-paste mistake by the page author. From the
    > parameter lists I would think that the second function was supposed to
    > be called "storeCookieKey" but the author copied the first function,
    > changed the body and the parameter list, but forgot to change the name
    > as well. The error is not apparent because the second function
    > definition replaces the first and the resulting storeCokie function is
    > error free when called with the parameters intended for the first. On
    > the other hand the function does not appear to ever be called by code on
    > that page so neither should really be there. But then, there is so much
    > JavaScript on that page that probably the whole lot should have been
    > moved into an external JS file to cut down on the download, both by
    > having it cached client side and for the users of non-JavaScript
    > browsers who would not need it at all.
    >
    > Incidentally, as you admit to not knowing much about JavaScript, may I
    > recommend that you not promote <URL: http://javascript.internet.com >
    > as, while they may make thousands of simple scripts available, there
    > does not appear to be any informed vetting of those scripts and as far
    > as I could tell none are good and most are extremely poor and not even
    > close to a suitable standard for Internet use.
    >
    > Richard.
    >
    >


    Thanks - With regards to the URL I had recommended in another post, I'll
    take your comments that some/all of their scripts are "extremely poor" but
    they have introduced me to Javascript, and helped me (and I'm sure others)
    to take the effort and find out more on how to create their own code, as
    opposed to borrowing from others on sites like the one I recommended.

    Cheers
    Randell D.
    Randell D., Nov 3, 2003
    #3
  4. > >function storeCookie(key,val) {
    > > var age = 100*24*3600 * 1000; // ms in 100 days
    > > storeCookieKey( key, val, age, true );
    > >}//storeCookie
    > >
    > >
    > >function storeCookie(key,val,age,reloadPg) {
    > > var expDate = new Date();
    > > var expTime = expDate.getTime() + age;
    > > expDate.setTime( expTime );
    > > document.cookie = key +"="+ val + "; expires="+

    > expDate.toGMTString() ;
    > > if( reloadPg )
    > > window.location.reload();
    > >}//storeCookie

    > <snip>
    >
    > It looks like a cut-and-paste mistake by the page author. From the
    > parameter lists I would think that the second function was supposed to
    > be called "storeCookieKey" but the author copied the first function,
    > changed the body and the parameter list, but forgot to change the name
    > as well. The error is not apparent because the second function
    > definition replaces the first and the resulting storeCokie function is
    > error free when called with the parameters intended for the first.


    jslint will detect this error. http://www.crockford.com/lint.html
    Douglas Crockford, Nov 3, 2003
    #4
  5. "Douglas Crockford" <> wrote in message
    news:8b324$3fa66749$44a4af49$...
    <snip>
    >jslint will detect this error.
    > http://www.crockford.com/lint.html


    The URL seems to work better with:-

    <URL: http://www.crockford.com/javascript/jslint.html >

    But as you mentioned it, I was reading <URL:
    http://www.crockford.com/javascript/lint.html > over the weekend and
    while I understand (and approve of) most of the additional restrictions
    JSLINT places on JavaScript source code, I did wonder if it was
    necessary to absolutely prevent - switch - statements from using
    fall-through. I realise that in almost all cases omitting the - break; -
    would be an error but I have always though that one of the things that
    distinguished - switch - from the stack of - if-else if - statements for
    which it is an obvious alternative is the ability to use it to provide
    several conditional entry points into a process by allowing code
    executed following early case statements to fall-through to code that
    may otherwise only be executed under different conditions.

    You may know of a sound reason for never doing that, but otherwise
    couldn't JSLINT have a setting to down grade fall-through in - switch -
    from a fail to a warning?

    Richard.
    Richard Cornford, Nov 3, 2003
    #5
  6. In article <8b324$3fa66749$44a4af49$>,
    Douglas Crockford <> writes
    <snip>
    >jslint will detect this error. http://www.crockford.com/lint.html


    Note that re-using a function name is legal, but is also very likely to
    be a typing error.

    John
    --
    John Harris
    John G Harris, Nov 3, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    447
  2. athiane
    Replies:
    13
    Views:
    515
    Netocrat
    Mar 11, 2006
  3. Lee Xuzhang
    Replies:
    5
    Views:
    330
    Kevin D. Quitt
    Jun 14, 2006
  4. 4MLA1FN
    Replies:
    10
    Views:
    575
    4MLA1FN
    May 8, 2006
  5. NM
    Replies:
    6
    Views:
    463
    Default User
    Sep 20, 2006
Loading...

Share This Page