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

Re: multiple parameters in function

P: n/a
jodleren wrote:
On Jul 2, 12:04 pm, jodleren <sonn...@hot.eewrote:
>I want to pass one or two parameters to a function, such as

function OpenDirSel(input, user)
{
}

The point: it does not work with 2 parameters, why?
Is there a way, that I can make the 2nd parameter optional?
ECMAScript functions do not have arity (a fixed number of arguments that
they take). This means *all* arguments are optional on call. Whether they
are also implemented as being optional, depends on you/the function's
developer. For example:

function openDirSel(input, user)
{
/*
* Gauntlet to return if the argument value is a false-value;
* or you could throw an exception etc.
*/
if (!user) return null;

window.alert(input);
}

or

function openDirSel(input)
{
/*
* Gauntlet to return if the argument value is a false-value;
* no *named* second argument, so we use the `arguments' object.
*/
if (!arguments[1]) return null;

window.alert(input);
}

and then

if (!openDirSel("BOO!"))
{
// DEBUG: `undefined' converts to `false'
window.alert("openDirSel: Invalid second argument!");
}
Like in
<input name="btn_dir1" type="button" value=" ... "
onclick="OpenDirSel(dir1.value, 'test');">
It would not work because `dir1' did not refer to an object. ISTM you were
looking for `this' instead, to refer to the object handling the event.

Or your markup could be not Valid, so there was no script code to execute:
<http://validator.w3.org/>
PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
Jul 2 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Thomas 'PointedEars' Lahn wrote:
jodleren wrote:
>On Jul 2, 12:04 pm, jodleren <sonn...@hot.eewrote:
>>I want to pass one or two parameters to a function, such as

function OpenDirSel(input, user)
{
}

The point: it does not work with 2 parameters, why?
Is there a way, that I can make the 2nd parameter optional?

ECMAScript functions do not have arity (a fixed number of arguments that
they take). This means *all* arguments are optional on call. Whether they
are also implemented as being optional, depends on you/the function's
developer. For example:

function openDirSel(input, user)
{
/*
* Gauntlet to return if the argument value is a false-value;
* or you could throw an exception etc.
*/
if (!user) return null;

window.alert(input);
}

[...]
and then

if (!openDirSel("BOO!"))
{
// DEBUG: `undefined' converts to `false'
window.alert("openDirSel: Invalid second argument!");
}
Hmmm, to explain better, I should have returned `true' if all necessary
arguments were passed:

function openDirSel(input, user)
{
/*
* Gauntlet to return if the argument value is a false-value;
* or you could throw an exception etc.
*/
if (!user) return false;

window.alert(input);
return true;
}
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jul 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.