By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,528 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,528 IT Pros & Developers. It's quick & easy.

Duplicate funtion names - what happens?

P: n/a

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

Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Randell D." <yo**************************@yahoo.com> 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.
Jul 20 '05 #2

P: n/a

"Richard Cornford" <Ri*****@litotes.demon.co.uk> wrote in message
news:bo*******************@news.demon.co.uk...
"Randell D." <yo**************************@yahoo.com> 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.
Jul 20 '05 #3

P: n/a
> >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

Jul 20 '05 #4

P: n/a
"Douglas Crockford" <no****@laserlink.net> wrote in message
news:8b**************************@msgid.meganewsse rvers.com...
<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.
Jul 20 '05 #5

P: n/a
In article <8b**************************@msgid.meganewsserver s.com>,
Douglas Crockford <no****@laserlink.net> 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
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.