-
Jan 6th, 2012, 02:25 PM
#1
Thread Starter
Member
Need code explanation
Can someone please explain this code to me:
Why does the funtion call below take in two arguments:
type = Spry.Widget.Utils.firstValid(type, "none");
While it was defined without any argument:
Spry.Widget.Utils.firstValid = function() {
var ret = null;
for(var i=0; i<Spry.Widget.Utils.firstValid.arguments.length; i++) {
if (typeof(Spry.Widget.Utils.firstValid.arguments[i]) != 'undefined') {
ret = Spry.Widget.Utils.firstValid.arguments[i];
break;
}
}
return ret;
};
Any help and thought wellcomed.
Thanks.
-
Jan 6th, 2012, 05:32 PM
#2
Re: Need code explanation
Just part of how Javascript works; you're not required to specify all (or any) of a function's arguments. If you write a function that's expected to have a consistent number of arguments, then it's good practice to (and simpler to work with if you) specify them. But if your function may accept a variable number of arguments, then you might use something like what you've got there; all functions have an "arguments" object by which you can access the passed arguments by numeric index.
It's not an approach you should use without good reason though.
Last edited by SambaNeko; Jan 6th, 2012 at 05:37 PM.
-
Jan 6th, 2012, 05:51 PM
#3
Thread Starter
Member
Re: Need code explanation
Thanks SambaNeko, u've just saved me 3 days of wondering. Thank you soo much.
-
Jan 7th, 2012, 06:28 AM
#4
Re: Need code explanation
It's not necessary to qualify arguments:
Code:
Spry.Widget.Utils.firstValid = function() {
var ret = null;
for(var i=0; i<arguments.length; i++) {
if (typeof(arguments[i]) != 'undefined') {
ret = arguments[i];
break;
}
}
return ret;
};
-
Jan 7th, 2012, 06:32 AM
#5
Re: Need code explanation
another improvement:
Code:
Spry.Widget.Utils.firstValid = function() {
for (var i in arguments)
if (typeof arguments[i] !== 'undefined')
return arguments[i];
};
Not really sure of the value of this function, to be frank.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|