Duplicate funtion names - what happens?

R

Randell D.

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
 
R

Richard Cornford

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.
 
R

Randell D.

Richard Cornford said:
<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.
 
D

Douglas Crockford

function storeCookie(key,val) {
<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
 
R

Richard Cornford


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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top