How to pass the value 'undefined' to a function with multiple parameters?

痴心易碎 提交于 2019-11-28 21:02:55
xdazz

myFunction(undefined,"abc"); this way should work, what is the problem?

see here

Here is undefined documentation from mozilla, supported by all browsers

The void operator seems to be the most common way to explicitly get undefined.

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:

var x;
if (x === void 0) {
    // this will execute
}
dbrin

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

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

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.

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'});

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

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 );

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

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:

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).

If its possible , could you rearrange the function as myFunction (parm2, parm1)

ie. try to ensure possible undefined parameters are last in the function , this might not work if more parameters are introduced.

An easy way of doing this, when acceptable is to pass undefined. But better yet as follows per W3C

Javascript-missing arguments

function myFunction(x, y) {
if (y === undefined) {
      y = 0;
    } 
}

Just to give an examples on what @dbrin was explaining, and @alphapilgrim might have wished to check.

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];    
      }

    });
 
  
  }
  
 

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!