Is it possible to set default arguments for functions like you are able to in PHP?
Like so: -
function(type = 'default', default = 'argument')
-
// ...
-
?
Cheers guys
7 1912 gits 5,390
Expert Mod 4TB
hi ...
not like that ... but you may use: - function my_func(arg0) {
-
// if arg0 is not passed to my_func
-
if (typeof arg0 == 'undefined') {
-
arg0 = 'default_val0';
-
}
-
-
// now use arg0 and further code
-
}
kind regards
hi ...
not like that ... but you may use: - function my_func(arg0) {
-
// if arg0 is not passed to my_func
-
if (typeof arg0 == 'undefined') {
-
arg0 = 'default_val0';
-
}
-
-
// now use arg0 and further code
-
}
kind regards
Yeh, thought that might be the case.
Was just looking for a prettier alternative ^_^
This should hopefully be supported in the next major version of JavaScript/ECMAScript or something similar. In the meantime, you could use something like this.
- function shout(message){
-
alert( message || "hello world" )
-
}
That's effective in most cases,but if you pass null, false, 0, empty string, etc. it will return "hello world" which is probably not what you want.
That's effective in most cases,but if you pass null, false, 0, empty string, etc. it will return "hello world" which is probably not what you want.
thats a good point. any falsy -evaluating variable would trigger the default.
if you want the defaults only upon undefineds, try something like: -
function shout(message){
-
alert( message != undefined ? message : "hello world" )
-
}
-
- function (message){
-
message= (message !== undefined)? message : "default" ;
-
}
!== not equal and same type
Sign in to post your reply or Sign up for a free account.
Similar topics
by: J.R. |
last post by:
Hi folks,
The python can only support passing value in function call (right?), I'm
wondering how to effectively pass a large parameter, such as a large list or
dictionary?
It could achieved...
|
by: Edward Diener |
last post by:
In the tutorial on functions there are sections on default arguments and
keyword arguments, yet I don't see the syntactic difference between them.
For default arguments the tutorial shows:
def...
|
by: Nick Coghlan |
last post by:
Time for another random syntax idea. . .
So, I was tinkering in the interactive interpreter, and came up with the
following one-size-fits-most default argument hack:
Py> x = 1
Py> def...
|
by: Agent Mulder |
last post by:
Hi group,
I want to know why this doesn't work
void f(int a=0,int b=1,int c=2){}
int main()
{
f(); //OK
f(1); //OK
f(1,2); //OK
f(1,2,3); //OK
|
by: earl |
last post by:
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix;
};
temp::foo(char p, char o, char m = matrix )
|
by: The Directive |
last post by:
This code will not compiled:
In main function:
Dot temp = new Dot( *(new Point( 5, 5 )) );
In Dot class:
//Constructor with default arguments.
Dot::Dot( Point& point= *(new Point( 5, 5...
|
by: aling |
last post by:
What's the rule of default argument of function in C++? I found that
the default argument of function could not necessary be a constant
value. Is it true? Previously I thought that the default...
|
by: sods |
last post by:
Hi,
I write a test code about template used for strategy.
it's very similar to sample code in TC++PL 13.4.1.
#include <iostream>
#include <string>
using std::basic_string;
|
by: claudiu |
last post by:
Hi,
I'll go straight to the first question. Why does the code below
compile?
void f(int i = 0);
int main()
{
(&f)();
|
by: bukzor |
last post by:
I've found some bizzare behavior when using mutable values (lists,
dicts, etc) as the default argument of a function. I want to get the
community's feedback on this. It's easiest to explain with...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
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...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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...
| |