473,395 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Default function parameter

Hi,

Is there a way to provide a default for a function parameter.
i tried
function func (type, message, obj = "message")
but it doesn't seem to work.
It seems a bit ugly to make an if/else statement right at the begining
of a function, just to imitate this behavior.
Jul 23 '05 #1
8 1763
Börni wrote:
Is there a way to provide a default for a function parameter.
i tried
function func (type, message, obj = "message")
but it doesn't seem to work.


I usually use this way to make default parameters:
function func (type, message, obj) {
obj = (typeof obj == "undefined") ? "message" : obj;

or just
obj = (!obj) ? "message" : obj;
but I think IE5 (or whatever) has a problem with that.
Jul 23 '05 #2
On Thu, 23 Dec 2004 20:20:31 +0000, Börni <b.******@onlinehome.de> wrote:
Is there a way to provide a default for a function parameter.


Typically, this is done with:

function myFunction(arg) {arg = arg || ???;
/* ... */
}

where ??? is your default value.

The logical OR operator is a bit different in ECMAScript when compared to
other languages. Normally,

opr1 || opr2

would evaluate to true or false, depending on whether either opr1 or opr2
is true. In ECMAScript, the operands are tested for true- or "falseness",
but it's their values that are actually returned. Consider:

false || 2

The literal, false, will obviously evaluate to false so the result of the
expression is 2. With:

'hello' || 'world'

the non-empty string, 'hello', will evaluate to true so the result of the
expression is 'hello'.

If code called the function above with an argument that evaluated to false
(null, undefined, false, 0, or ''), or no argument at all, the second
operand would be assigned to arg. Of course, if one of those values is a
legal argument that shouldn't be changed, this particular approach isn't
suitable. In that case, you can use Ulrik's first suggestion.

[snip]

Hope that helps,
Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #3
> Is there a way to provide a default for a function parameter.
i tried
function func (type, message, obj = "message")
but it doesn't seem to work.


Of course it doesn't work. You can't make up your own syntax.

You can use the default operator.

a || b

If a is falsy (null, undefined, 0, or empty string) then the value is a.
Otherwise it is b. Missing parameters get the value undefined, which is
falsy.

http://www.crockford.com/javascript/survey.html
Jul 23 '05 #4
Ivo
"Michael Winter" said
Börni asked
Is there a way to provide a default for a function parameter


Typically, this is done with:

function myFunction(arg) {arg = arg || ???;
/* ... */
}

where ??? is your default value.


Probably the shortest syntax is:

function myFunction(arg) { arg |= ???;

When no arg is provided, it will be of type 'undefined'. To catch the
occasion when it may actually be false, null, an empty string or zero, you
need to include a typeof test.

HTH
--
Ivo
http://www.ariel.shakespearians.com/
Jul 23 '05 #5
On Fri, 24 Dec 2004 01:54:05 +0100, Ivo <no@thank.you> wrote:

[snip]
Probably the shortest syntax is:

function myFunction(arg) { arg |= ???;


It might be the shortest syntax, but it's not very appropriate. That
operation is performed in terms of 32-bit integers only. Moreover,
consider when the operand is another number other than zero. If arg is
zero, the default will be set to the operand as expected, but if arg is
non-zero it will be assigned the value of (arg | operand), which will
mutate the argument in what is probably an undesirable way.

Unfortunately, the production

arg ||= def;

doesn't exist.

[snip]

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6
function myFunction(arg) {arg = arg || ???;
/* ... */
}


I took the above approach as i can be sure that i never will pass an
argument that contains 0, undefined...
( it gets passed the id of an html element )
Jul 23 '05 #7
Ivo
"Ivo" wrote
"Michael Winter" said
Börni asked
Is there a way to provide a default for a function parameter


Typically, this is done with:

function myFunction(arg) {arg = arg || ???;
/* ... */
}

where ??? is your default value.


Probably the shortest syntax is:

function myFunction(arg) { arg |= ???;

When no arg is provided, it will be of type 'undefined'. To catch the
occasion when it may actually be false, null, an empty string or zero, you
need to include a typeof test.


Slightly related, to remember the arg used last time the function was
called, but without creating a global variable, use "this". In the following
example, the function "myFunc" gets its argument "p" from:
1. where ever it is called from, or -if called without argument-
2. a prompt box, with a default value of:
1. the argument used last time myFunc ran, or -if this is the first time-
2. 'myArgument'.

function myFunc(p) {
p= p || window.prompt( 'Enter your argument:', this.last ||
'myArgument' );
if(p) {
this.last = p;
/* ... */
}
}

// this will invoke prompt box:
someElement.onclick = myFunc;

// this will use 'someargument' :
someotherElement.onclick = function() { myFunc( 'someArgument' ); };

Other functions may feature their own "this.last" history without
interference. It works for me, although I don't know whether "this" is the
calling element or the function scope itself...
--
Ivo
Jul 23 '05 #8
On Fri, 24 Dec 2004 13:58:39 +0100, Ivo <no@thank.you> wrote:

[snip]
// this will invoke prompt box:
someElement.onclick = myFunc;

// this will use 'someargument' :
someotherElement.onclick = function() { myFunc( 'someArgument' ); };

Other functions may feature their own "this.last" history without
interference. It works for me, although I don't know whether "this" is
the calling element or the function scope itself...


It depends how myFunc is called. With the first approach where myFunc is,
and will be called as, a method of someElement, this will refer to that
element. With the second where myFunc is called directly, this will refer
to the global object.

Mike

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Alex Panayotopoulos | last post by:
Hello all, Maybe I'm being foolish, but I just don't understand why the following code behaves as it does: - = - = - = - class listHolder: def __init__( self, myList= ): self.myList =...
49
by: Mark Hahn | last post by:
As we are addressing the "warts" in Python to be fixed in Prothon, we have come upon the mutable default parameter problem. For those unfamiliar with the problem, it can be seen in this Prothon...
18
by: Dan Cernat | last post by:
Hi there, A few threads I had a little chat about default values. I am starting this thread because I want to hear more opinions about the default values of function parameters. Some say they...
3
by: Schwarzbauer Günter | last post by:
Hello, Does the C++ standard allow default parameters when defining a typedef for a function type, e.g. typedef void (*TYPE_Report)(const char* message, const char* details = 0); This was...
2
by: Ekim | last post by:
hy, I've got a question according default parameters of functions that are accessed from different files: ---- logging.h ---- void LogTrace(const char* errorLocation, const char* errorInfo =...
2
by: Vic Y | last post by:
Hi, I am trying to call a user defined function(UDF) from a stored proc, using a default parameter in the called UDF (e.g. @bid_price_type int = 0 ). However the calling stored proc complains...
11
by: Matthias Pfeifer | last post by:
Hi there, I am trying to declare a function that takes a std::list<parameter. I want this function to have an empty list as a default parameter. It's a template function also. Currently i am...
1
by: abedford | last post by:
Hi, I would like to add a default parameter to a COM function. I defined it in the IDL file like this: HRESULT GetName( BSTR id1 , BSTR id2 , ...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
7
by: jamesclose | last post by:
My problem is this (apologies if this is a little long ... hang in there): I can define a function in VB.NET with optional parameters that wraps a SQL procedure: Sub Test(Optional ByVal Arg1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.