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

发布于 2024-12-10 05:05:30 字数 793 浏览 1 评论 0原文

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

myFunc( 'first' );

var obj = { a: 123 };
myFunc( 'first', obj.b );
_or_
myFunc( 'first', undefined )

arguments.length 引用过去命名参数的参数,因此没有帮助 可以使用 arguments.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 中捕获 Case ACase B 的正确方法是什么?

within a function, how to discern between a non-arg and an undefined arg?

myFunc( 'first' );

var obj = { a: 123 };
myFunc( 'first', obj.b );
_or_
myFunc( 'first', undefined )

arguments.length refers to arguments past the named arguments, so it's no help Can be solved easily with arguments.length - sorry about the brain fart!

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' ) { ... }

  ...
}

What is the correct way to capture Case A and Case B in myFunc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去 2024-12-17 05:05:30

尝试类似的方法:

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

Try something like:

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);
情场扛把子 2024-12-17 05:05:30

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

让我们看看您希望如何使用 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()。在所有其他情况下,应调用 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') { ... }     
   ...
 }

I believe there's no cross-browser compatible way to do exactly what you wish. Moreover, I think that a function that changes its behavior when undefined is passed explicitly (vs. not passed at all) is confusing. That said, your overall target can be achieved by slightly changing the protocol.

Let's examine how you want my_func to be used:

my_func(1) // case A
my_func(1, undefined) // case B
my_func(1, {}.b) // case B
my_func(1, "blah") // case C

See my point? Case A can happen only if the caller passes a single argument.

Thus, if you separate my_func() into two functions: my_funcA, taking a single argument, and my_funcBC, taking two arguments, you will be able to implement your logic correctly.

The only change that this inflicts, on the caller of the function, is that if the caller passes a single argument he needs to call my_funcA(). In all other casses, my_funcBC() should be called.

 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') { ... }     
   ...
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文