HighDots Forums  

Re: multiple parameters in function

Javascript JavaScript language (comp.lang.javascript)


Discuss Re: multiple parameters in function in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: multiple parameters in function - 07-02-2008 , 06:10 AM






jodleren wrote:
Quote:
On Jul 2, 12:04 pm, jodleren <sonn... (AT) hot (DOT) ee> wrote:
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!");
}

Quote:
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


Reply With Quote
  #2  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: multiple parameters in function - 07-02-2008 , 08:00 AM






Thomas 'PointedEars' Lahn wrote:
Quote:
jodleren wrote:
On Jul 2, 12:04 pm, jodleren <sonn... (AT) hot (DOT) ee> wrote:
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


Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.