Closed Thread Icon

Topic awaiting preservation: simple javascript function question Pages that link to <a href="https://ozoneasylum.com/backlink?for=8118" title="Pages that link to Topic awaiting preservation: simple javascript function question" rel="nofollow" >Topic awaiting preservation: simple javascript function question\

 
Author Thread
maninacan
Paranoid (IV) Inmate

From: Seattle, WA, USA
Insane since: Oct 2001

posted posted 03-14-2002 20:22

ok so I have a javascript function. It does a lot of different things, and for one of this things I want to be able to include another argument, but none of the others need it, so it would be a hassle to have it in every call of the function, so I was wondering if there was a way I could make the argument optional. I know there's ways to do this in other scripting languages, but I've never come across it in javascript. Does it exist, and if so how would I accomplish this?

http://www.kewlster.com

Pugzly
Paranoid (IV) Inmate

From: 127.0.0.1
Insane since: Apr 2000

posted posted 03-14-2002 21:12

Post your code - that may help us.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 03-14-2002 21:16

In JavaScript, all arguments are automatically optional. If an argument isn't specified, it will have the undefined value inside the function. For instance, the following function

function myfunc(a, b, c)
{
...
}

can be called in any of the following ways:

myfunc(); // a b and c will be undefined
myfunc(1,2); // c will be undefined
myfunc(1,,3); // b will be undefined
myfunc(1,2,3,4,5,6,7); a, b, and c will be defined; to get access to the rest of the arguments, use the arguments[] array accessed from inside the function.

maninacan
Paranoid (IV) Inmate

From: Seattle, WA, USA
Insane since: Oct 2001

posted posted 03-14-2002 21:17

Thanks Slime,
I didn't know that.

http://www.kewlster.com

Bugimus
Maniac (V) Mad Scientist

From: New California
Insane since: Mar 2000

posted posted 03-16-2002 05:21

Javascript is just that... "script". It is much less strict than full up programming languages like c++. And thankfully so for what it's intended to do.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 03-16-2002 05:41

On the other hand, that enables it to have things like the eval() function, which are amazingly overused... and it's slower too; much slower than a real, compiled, language. So, advantages and disadvantages, really. Compiled languages tend to have more low-level things that give you ways of getting around the lack of an eval() function or the [] operator for indirection.

« BackwardsOnwards »

Show Forum Drop Down Menu