简体   繁体   English

如何将我通过函数传递的变量绑定到特定参数

[英]How to bound variable which I pass through function to a specific parameter

Very stupid question.非常愚蠢的问题。 For example I have a variable data and I have function DoSomething with two parameters par1 and par2.例如,我有一个变量数据,并且我有带有两个参数 par1 和 par2 的函数 DoSomething。 they are both optional parameters that would be null if I pass nothing.它们都是可选参数,如果我什么都不传递,它们将为空。 But I have only one variable data which I want to bound to the second parameter without touching first parameter.但是我只有一个变量数据,我想绑定到第二个参数而不触及第一个参数。 In different language I should write something like DoSomething(par2 = data) or DoSomething(par2: data) but all these examples will raise exception Uncaught SyntaxError: missing ) .用不同的语言我应该写类似DoSomething(par2 = data)DoSomething(par2: data)但所有这些例子都会引发异常Uncaught SyntaxError: missing ) So what I did wrong?那我做错了什么?

If you have two optional parameters in JavaScript, just pass undefined to the first argument.如果 JavaScript 中有两个可选参数,只需将undefined传递给第一个参数。

DoSomething(undefined, data);

undefined is the default value of arguments when they aren't passed in ( DoSomething() would have both arguments be undefined ) except when you set default values for optional arguments. undefined是未传入参数时的默认值( DoSomething()将使两个参数都undefined ),除非您为可选参数设置默认值。

Another alternative is to pass in the arguments as an object, which requires changing the definition of the function and all calls to it: ( ?? is the Nullish coalescing operator另一种选择是将参数作为对象传递,这需要更改函数的定义以及对它的所有调用:( ??Nullish 合并运算符

function DoSomething(args) {
  const par1 = args.par1 ?? "default value";
  const par2 = args.par2 ?? "default value";
}

// ....
DoSomething({ par2: "custom value" });
DoSomething({ par1: "custom value", par2: "customValue" });

Using TypeScript or JSDocs allows you to give guidance on how to send arguments in the IDE使用TypeScriptJSDocs可以指导您如何在 IDE 中发送参数

You always can pass undefined or null as first parameter.您始终可以将undefinednull作为第一个参数传递。 SO it will be something like this:所以它会是这样的:

DoSomething(undefined, data);

If you have multiple optional parameters, if you don't want to pass undefined.. you should use an object as the parameter.如果你有多个可选参数,如果你不想传递 undefined.. 你应该使用一个对象作为参数。

// instead of DoSomething(param1, param2)
function DoSomething(param = {}) {
  const { param1, param2 } = param;

  //..do something with param1 and param2
}

DoSomething({ param2: 'somevalue' })

If you consider using typescript, it's even better because you can add the type to the param.如果您考虑使用打字稿,那就更好了,因为您可以将类型添加到参数中。

First of all, if you don't pass a parameter, it would be undefined , not null .首先,如果您不传递参数,它将是undefined ,而不是null

If you want to skip the first parameter, you still have to pass it in, just pass it in as something falsy (assuming your doSomething function handles that case).如果你想跳过第一个参数,你仍然需要传入它,只需将它作为 falsy 传入(假设你的doSomething函数处理这种情况)。

 const data = 'foo'; function doSomething(param1, param2) { if (param1) console.log(param1); if (param2) console.log(param2); } doSomething(null, data);

If you a similar functionality to Python, you can take in a single parameter, which is an object.如果你有与 Python 类似的功能,你可以接受一个参数,它是一个对象。

 const data = 'foo'; function doSomething({ param1, param2 }) { if (param1) console.log(param1); if (param2) console.log(param2); } doSomething({ param2: data });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM