I want to pass the value of 'undefined' on a multiple parameter function but without omitting the parameter.
我想在多参数函数上传递'undefined'的值但不省略参数。
What do I mean with "without omitting the parameter". I mean that we should not just omit the parm2
like this example:
“没有省略参数”是什么意思。我的意思是我们不应该像这个例子那样省略parm2:
function myFunction (parm1, parm2) {}
myFunction("abc");
This will indeed make parm2
undefined, but however I am not allowed to do it this way because I will need to specify other parameters AFTER the omitted parameter.
这确实会使parm2未定义,但是我不允许这样做,因为我需要在省略的参数之后指定其他参数。
So in the case I want to make parm1
undefined BUT also want to have other parameters after this one to hold a value won't work with the previous method.
所以在这种情况下我想让parm1未定义但是也希望在此之后有其他参数来保存一个值将无法使用前一个方法。
I have tried with:
我尝试过:
myFunction( ,"abc"); //don't seem to work
And:
和:
myFunction(undefined,"abc"); //neither work
12 个解决方案
#1
11
A better approach might be passing Object
with named attributes and then look for those specific attribute values. This way you don't have to be dependent on number of arguments.
Example: Object.dummy
更好的方法可能是使用命名属性传递Object,然后查找这些特定的属性值。这样您就不必依赖于参数的数量。示例:Object.dummy
#2
47
myFunction(undefined,"abc");
this way should work, what is the problem?
myFunction的(未定义, “ABC”);这种方式应该有效,有什么问题?
see here
看这里
Here is undefined documentation from mozilla, supported by all browsers
这是来自mozilla的未定义文档,受所有浏览器支持
#3
12
The void
operator seems to be the most common way to explicitly get undefined
.
void运算符似乎是显式获取未定义的最常用方法。
You would use it like this in your example:
你可以在你的例子中使用它:
myFunction(void 0, "abc");
It's also a reliable method for comparing against undefined
that is guarded against undefined
being accidentally overridden in older JavaScript environments:
它也是一种可靠的方法,用于比较未定义的,以防止在旧的JavaScript环境中意外覆盖的未定义:
var x;
if (x === void 0) {
// this will execute
}
#4
5
I just had an idea and it seems to work:
我只是有一个想法,它似乎工作:
var undf;
myFunction(undf, "abc");
I am sure there are better ways, however I post this
我确信有更好的方法,但我发布这个
#5
1
I think the closest you'll get to this is passing null
as a parameter. It's not undefined
, but for most cases it's close enough.
我认为你最接近的是传递null作为参数。它并不是未定义的,但对于大多数情况来说它足够接近。
#6
1
Try to use this method if you plan on adding an indefinite amount of parameters:
如果您计划添加无限量的参数,请尝试使用此方法:
function myFunc(params) {
// Define default values
var name = 'John';
var age = '40';
// You can loop through them
for (var p in params) {
alert(p + ':' + params[p]);
}
// And read them in like this
if (typeof params.name != 'undefined') {
name = params.name;
}
if (typeof params.age != 'undefined') {
age = params.age;
}
alert(name + ' ' + age);
}
alert('test1');
myFunc({name:'Bob', age:'30'});
alert('test2');
myFunc({name:'Bob'});
#7
1
You can use apply and an array of parameters to pass "undefined" as one of the parameters. For example, you wanted to pass parm1 as "undefined":
您可以使用apply和一组参数将“undefined”作为参数之一传递。例如,您希望将parm1作为“undefined”传递:
function myFunction (parm1, parm2) {
if(typeof (parm1) === "undefined"){
alert("parm1 is undefined")
}
if(typeof (parm2) === "undefined"){
alert("parm2 is undefined")
}
}
var myParameters = [undefined, "abc"];
myFunction.apply(valueForThis, myParameters );
#8
1
You need to handle the function call with undefined variable in the function code itself in one of the following way :-
您需要使用以下方法之一在函数代码中处理带有未定义变量的函数调用: -
function myFunction (parm1, parm2) {
if (parm1 !== undefined) {
// execute code if 1st param is undefined
}
if (parm2 !== undefined) {
// execute code if 2 param is undefined
}
// execute other part of code
}
Now you can call the above function in following ways:-
现在您可以通过以下方式调用上述功能: -
myFunction(undefined,"abc"); // with 1st param value not known
myFunction("cde",undefined); // with 2nd param value not known
myFunction("cde","abc"); // with both param value known
#9
0
myFunction(undefined,"abc")
should absolutely work, unless someone cruel has redefined undefined
! If you want to be safe, there are dozens of ways to get the undefined value which avoid the assumption that the thing called "undefined" does in fact have the special undefined value:
myFunction(undefined,“abc”)应该绝对有效,除非有人残忍地重新定义了undefined!如果你想要安全,有很多种方法可以获得未定义的值,这可以避免假设名为“undefined”的东西确实具有特殊的未定义值:
void 0
var undef; undef
[][0]
{}.foo
// `undef` is guaranteed to have the undefined value within this closure
(function(undef){ undef }())
// this expression evaluates as undefined
(function(){}())
void 0
is the most widely used (compiled CoffeeScript includes this wherever the undefined value is required).
void 0是使用最广泛的(编译的CoffeeScript包含需要未定义值的任何地方)。
#10
0
If its possible , could you rearrange the function as myFunction (parm2, parm1)
如果可能的话,你可以将函数重新排列为myFunction(parm2,parm1)
ie. try to ensure possible undefined parameters are last in the function , this might not work if more parameters are introduced.
即。尝试确保可能的未定义参数是函数中的最后一个,如果引入了更多参数,这可能不起作用。
#11
0
An easy way of doing this, when acceptable is to pass undefined. But better yet as follows per W3C
一个简单的方法,在可接受的情况下通过undefined。但是对于W3C来说,更好的是如下
缺少Javascript的参数
function myFunction(x, y) {
if (y === undefined) {
y = 0;
}
}
#12
0
Just to give an examples on what @dbrin was explaining, and @alphapilgrim might have wished to check.
只是举一个关于@dbrin解释的例子,@ alphapilgrim可能希望检查。
simpleFunction({params1:12, params2:"abc"}); //use expected arguments
simpleFunction({params2:"abc"}); //use only one
simpleFunction({params2:"abc", params1:12, iDoNotExist:"I will not be included"}); //use expected arguments in any order
simpleFunction(); //ignore arguments and uses defaults
simpleFunction(2123); //ignores non object arguments
function simpleFunction(someParams){
var myParams = {
params1:null,
params2:"cde" //default value for second parameter
};
simpleExtend(myParams, someParams);
console.log(myParams);
}
//to include in your utilities
function simpleExtend(currentParams, newParams){
if(typeof currentParams !== "undefined" && typeof newParams !== "undefined"){
Object.keys(newParams).forEach(function(key){
if(typeof currentParams[key] !== "undefined"){
currentParams[key] = newParams[key];
}
});
}
}
#1
11
A better approach might be passing Object
with named attributes and then look for those specific attribute values. This way you don't have to be dependent on number of arguments.
Example: Object.dummy
更好的方法可能是使用命名属性传递Object,然后查找这些特定的属性值。这样您就不必依赖于参数的数量。示例:Object.dummy
#2
47
myFunction(undefined,"abc");
this way should work, what is the problem?
myFunction的(未定义, “ABC”);这种方式应该有效,有什么问题?
see here
看这里
Here is undefined documentation from mozilla, supported by all browsers
这是来自mozilla的未定义文档,受所有浏览器支持
#3
12
The void
operator seems to be the most common way to explicitly get undefined
.
void运算符似乎是显式获取未定义的最常用方法。
You would use it like this in your example:
你可以在你的例子中使用它:
myFunction(void 0, "abc");
It's also a reliable method for comparing against undefined
that is guarded against undefined
being accidentally overridden in older JavaScript environments:
它也是一种可靠的方法,用于比较未定义的,以防止在旧的JavaScript环境中意外覆盖的未定义:
var x;
if (x === void 0) {
// this will execute
}
#4
5
I just had an idea and it seems to work:
我只是有一个想法,它似乎工作:
var undf;
myFunction(undf, "abc");
I am sure there are better ways, however I post this
我确信有更好的方法,但我发布这个
#5
1
I think the closest you'll get to this is passing null
as a parameter. It's not undefined
, but for most cases it's close enough.
我认为你最接近的是传递null作为参数。它并不是未定义的,但对于大多数情况来说它足够接近。
#6
1
Try to use this method if you plan on adding an indefinite amount of parameters:
如果您计划添加无限量的参数,请尝试使用此方法:
function myFunc(params) {
// Define default values
var name = 'John';
var age = '40';
// You can loop through them
for (var p in params) {
alert(p + ':' + params[p]);
}
// And read them in like this
if (typeof params.name != 'undefined') {
name = params.name;
}
if (typeof params.age != 'undefined') {
age = params.age;
}
alert(name + ' ' + age);
}
alert('test1');
myFunc({name:'Bob', age:'30'});
alert('test2');
myFunc({name:'Bob'});
#7
1
You can use apply and an array of parameters to pass "undefined" as one of the parameters. For example, you wanted to pass parm1 as "undefined":
您可以使用apply和一组参数将“undefined”作为参数之一传递。例如,您希望将parm1作为“undefined”传递:
function myFunction (parm1, parm2) {
if(typeof (parm1) === "undefined"){
alert("parm1 is undefined")
}
if(typeof (parm2) === "undefined"){
alert("parm2 is undefined")
}
}
var myParameters = [undefined, "abc"];
myFunction.apply(valueForThis, myParameters );
#8
1
You need to handle the function call with undefined variable in the function code itself in one of the following way :-
您需要使用以下方法之一在函数代码中处理带有未定义变量的函数调用: -
function myFunction (parm1, parm2) {
if (parm1 !== undefined) {
// execute code if 1st param is undefined
}
if (parm2 !== undefined) {
// execute code if 2 param is undefined
}
// execute other part of code
}
Now you can call the above function in following ways:-
现在您可以通过以下方式调用上述功能: -
myFunction(undefined,"abc"); // with 1st param value not known
myFunction("cde",undefined); // with 2nd param value not known
myFunction("cde","abc"); // with both param value known
#9
0
myFunction(undefined,"abc")
should absolutely work, unless someone cruel has redefined undefined
! If you want to be safe, there are dozens of ways to get the undefined value which avoid the assumption that the thing called "undefined" does in fact have the special undefined value:
myFunction(undefined,“abc”)应该绝对有效,除非有人残忍地重新定义了undefined!如果你想要安全,有很多种方法可以获得未定义的值,这可以避免假设名为“undefined”的东西确实具有特殊的未定义值:
void 0
var undef; undef
[][0]
{}.foo
// `undef` is guaranteed to have the undefined value within this closure
(function(undef){ undef }())
// this expression evaluates as undefined
(function(){}())
void 0
is the most widely used (compiled CoffeeScript includes this wherever the undefined value is required).
void 0是使用最广泛的(编译的CoffeeScript包含需要未定义值的任何地方)。
#10
0
If its possible , could you rearrange the function as myFunction (parm2, parm1)
如果可能的话,你可以将函数重新排列为myFunction(parm2,parm1)
ie. try to ensure possible undefined parameters are last in the function , this might not work if more parameters are introduced.
即。尝试确保可能的未定义参数是函数中的最后一个,如果引入了更多参数,这可能不起作用。
#11
0
An easy way of doing this, when acceptable is to pass undefined. But better yet as follows per W3C
一个简单的方法,在可接受的情况下通过undefined。但是对于W3C来说,更好的是如下
缺少Javascript的参数
function myFunction(x, y) {
if (y === undefined) {
y = 0;
}
}
#12
0
Just to give an examples on what @dbrin was explaining, and @alphapilgrim might have wished to check.
只是举一个关于@dbrin解释的例子,@ alphapilgrim可能希望检查。
simpleFunction({params1:12, params2:"abc"}); //use expected arguments
simpleFunction({params2:"abc"}); //use only one
simpleFunction({params2:"abc", params1:12, iDoNotExist:"I will not be included"}); //use expected arguments in any order
simpleFunction(); //ignore arguments and uses defaults
simpleFunction(2123); //ignores non object arguments
function simpleFunction(someParams){
var myParams = {
params1:null,
params2:"cde" //default value for second parameter
};
simpleExtend(myParams, someParams);
console.log(myParams);
}
//to include in your utilities
function simpleExtend(currentParams, newParams){
if(typeof currentParams !== "undefined" && typeof newParams !== "undefined"){
Object.keys(newParams).forEach(function(key){
if(typeof currentParams[key] !== "undefined"){
currentParams[key] = newParams[key];
}
});
}
}