Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Duplicate funtion names - what happens?

Reply
Thread Tools

Duplicate funtion names - what happens?

 
 
Randell D.
Guest
Posts: n/a
 
      11-03-2003

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



 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      11-03-2003
"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.


 
Reply With Quote
 
 
 
 
Randell D.
Guest
Posts: n/a
 
      11-03-2003

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


 
Reply With Quote
 
Douglas Crockford
Guest
Posts: n/a
 
      11-03-2003
> >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

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      11-03-2003
"Douglas Crockford" <> wrote in message
news:8b324$3fa66749$44a4af49$ 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.


 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      11-03-2003
In article <8b324$3fa66749$44a4af49$ s.com>,
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can I passing a funtion pointer to a FUNTION which point to THE FUNTION? Lee Xuzhang C Programming 5 06-14-2006 06:31 PM
Problem of finding funtion names in any C file athiane C Programming 13 03-11-2006 05:18 PM
Virtual Funtion Questions garyolsen C++ 3 12-02-2003 09:27 PM
Is there a good alternative to Outlook calendar/appointent funtion in a standalone program? say@no.to.spam Computer Support 13 11-26-2003 10:33 AM
Re: fraction reducing funtion Sam Holden C++ 2 08-04-2003 04:45 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57