SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
-
Jul 18, 2002, 06:22 #1
- Join Date
- Feb 2002
- Location
- Geneva, Switzerland
- Posts
- 74
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Creating a function with optional arguments
How can I create a function that takes optional arguments?
-ptolemy
-
Jul 18, 2002, 06:45 #2
- Join Date
- Feb 2002
- Location
- Gatwick, UK
- Posts
- 1,206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
All arguments are optional automatically.
function name(arguments){}
Flawless---=| If you're going to buy a pet - get a Shetland Giraffe |=---
-
Jul 18, 2002, 06:50 #3
- Join Date
- Feb 2002
- Location
- Geneva, Switzerland
- Posts
- 74
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Don't know how I never thought of that one...
Thanks,
-ptolemy
-
Jul 18, 2002, 06:52 #4
- Join Date
- Feb 2002
- Location
- Gatwick, UK
- Posts
- 1,206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Flawless---=| If you're going to buy a pet - get a Shetland Giraffe |=---
-
Jul 18, 2002, 08:24 #5
- Join Date
- Jul 1999
- Location
- SC, USA
- Posts
- 390
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hey,
Just remember that inside a function there is an array called "arguments" that you can use to access all arguments:
function myFunc(){
for(i=0;i<arguments.length;i++){
alert("arguments["+i+"] = " + arguments[i]);
}
}
myFunc("this",1,"IS",4,'U');
aDogModerator at www.javascriptcity.com/forums/
-
Jul 19, 2002, 01:12 #6
- Join Date
- Feb 2002
- Location
- Gatwick, UK
- Posts
- 1,206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Very true - except i rarely find an occasion that i need that.
However i VERY rarely have to pass arguments around.
The closest i do is to pass (event) around for Netscape
browsers. ( which don't recieve event properly )
Flawless---=| If you're going to buy a pet - get a Shetland Giraffe |=---
-
Jul 19, 2002, 06:14 #7
- Join Date
- Jul 1999
- Location
- SC, USA
- Posts
- 390
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
One way around that is to just go:
function handleEvents(){
event = arguments.callee.caller.arguments[0];
}
http://javascriptkit.com/dhtmltutors/nsevents.shtml
aDogModerator at www.javascriptcity.com/forums/
-
Jul 19, 2002, 06:19 #8
- Join Date
- Feb 2002
- Location
- Gatwick, UK
- Posts
- 1,206
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Granted - but not really all that worthwhile - is it...
Especially when the callee property doesn't always work
the way you want it to in all browsers.
You're far better off calling the function with handleEvent(event) and delcaring it in the function declaration just to be safe!
Flawless---=| If you're going to buy a pet - get a Shetland Giraffe |=---
Bookmarks