Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/410.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中,如何区分未传递的参数和未传递的未定义的参数_Javascript_Function_Syntax_Arguments - Fatal编程技术网

在javascript中,如何区分未传递的参数和未传递的未定义的参数

在javascript中,如何区分未传递的参数和未传递的未定义的参数,javascript,function,syntax,arguments,Javascript,Function,Syntax,Arguments,在函数中,如何区分非参数和未定义的参数 myFunc( 'first' ); var obj = { a: 123 }; myFunc( 'first', obj.b ); _or_ myFunc( 'first', undefined ) arguments.length指的是超过命名参数的参数,因此使用参数很难解决这一问题。length-很抱歉大脑放屁 function myFunc( a, b ) { // Case A: if no second arg, provide on

在函数中,如何区分非参数和未定义的参数

myFunc( 'first' );

var obj = { a: 123 };
myFunc( 'first', obj.b );
_or_
myFunc( 'first', undefined )
arguments.length
指的是超过命名参数的参数,因此使用
参数很难解决这一问题。length
-很抱歉大脑放屁

function myFunc( a, b ) {

  // Case A: if no second arg, provide one
  // should be: if( arguments.length < 2 ) ...
  if( b === undefined ) b = anotherFunc;

  // Case B: if b is not resolved - passed but undefined, throw
  else if( b === undefined ) throw( 'INTERNAL ERROR: undefined passed' );

  // Case C: if b not a function, resolve by name
  else if( typeof b != 'function' ) { ... }

  ...
}
函数myFunc(a,b){ //案例A:如果没有第二个参数,请提供一个 //应该是:if(arguments.length<2)。。。 如果(b==未定义)b=anotherFunc; //案例B:如果B未解决-通过但未定义,则抛出 如果(b==未定义)抛出('内部错误:未定义已通过'); //案例C:如果b不是函数,请按名称解析 else if(类型b!=“函数”){…} ... } 在
myFunc
中捕获案例A和案例B的正确方法是什么?

尝试以下方法:

function myFunc() {
  var a, b;

  if (arguments.length === 1) {
    a = arguments[0];
    console.log('no b passed');
  }
  else if (arguments.length > 1) {
    a = arguments[0];
    b = arguments[1];
    if (b === undefined) {
      console.log('undefined passed as parameter');
    }
  }

  console.log(a, b);
}

myFunc(1);
myFunc(1, undefined);
尝试以下方法:

function myFunc() {
  var a, b;

  if (arguments.length === 1) {
    a = arguments[0];
    console.log('no b passed');
  }
  else if (arguments.length > 1) {
    a = arguments[0];
    b = arguments[1];
    if (b === undefined) {
      console.log('undefined passed as parameter');
    }
  }

  console.log(a, b);
}

myFunc(1);
myFunc(1, undefined);

我相信没有一种跨浏览器兼容的方式可以完全满足您的需求。此外,我认为当显式传递
undefined
时改变其行为的函数(与根本不传递相比)令人困惑。也就是说,您的总体目标可以通过稍微更改协议来实现

让我们检查一下您希望如何使用my_func:

my_func(1) // case A
my_func(1, undefined) // case B
my_func(1, {}.b) // case B
my_func(1, "blah") // case C
明白我的意思吗?仅当调用方传递单个参数时,才会发生案例A

因此,如果您将
my_func()
分为两个函数:
my_funcA
,取一个参数,和
my_funcBC
,取两个参数,您将能够正确实现逻辑

这对函数的调用者造成的唯一变化是,如果调用者传递一个参数,他需要调用
my_funcA()
。在所有其他casse中,应调用
my_funcBC()

 function my_funcA(a) {
   my_funcBC(a, anotherFunc);
 }

 function my_funcBC(a, b) {
   if (b === undefined) 
     throw( 'INTERNAL ERROR: undefined passed' );

   if (typeof b != 'function') { ... }     
   ...
 }

我相信没有一种跨浏览器兼容的方式可以完全满足您的需求。此外,我认为当显式传递
undefined
时改变其行为的函数(与根本不传递相比)令人困惑。也就是说,您的总体目标可以通过稍微更改协议来实现

让我们检查一下您希望如何使用my_func:

my_func(1) // case A
my_func(1, undefined) // case B
my_func(1, {}.b) // case B
my_func(1, "blah") // case C
明白我的意思吗?仅当调用方传递单个参数时,才会发生案例A

因此,如果您将
my_func()
分为两个函数:
my_funcA
,取一个参数,和
my_funcBC
,取两个参数,您将能够正确实现逻辑

这对函数的调用者造成的唯一变化是,如果调用者传递一个参数,他需要调用
my_funcA()
。在所有其他casse中,应调用
my_funcBC()

 function my_funcA(a) {
   my_funcBC(a, anotherFunc);
 }

 function my_funcBC(a, b) {
   if (b === undefined) 
     throw( 'INTERNAL ERROR: undefined passed' );

   if (typeof b != 'function') { ... }     
   ...
 }

什么时候将
未定义的
作为函数参数传递?这不常见,是吗?这是图书馆。如果有人错误地使用了它,例如,传递了一个未定义的参数,则希望引发一个错误。为什么说
参数。长度
没有帮助?请看,无论参数如何变成
未定义的
,每次只处理案例A(或您的一个案例),这不是更好吗?事实上,传递未定义的应与不传递参数一样对待,您不应该在2之间做任何区别。您什么时候会将
未定义的
作为函数参数传递?这不常见,是吗?这是图书馆。如果有人错误地使用了它,例如,传递了一个未定义的参数,则希望引发一个错误。为什么说
参数。长度
没有帮助?请看,无论参数如何变成
未定义的
,每次只处理案例A(或您的一个案例),这不是更好吗?事实上,传递未定义的应与不传递参数一样对待,您不应该在2之间造成差异。