简体   繁体   English

如何在 javascript 中为命名参数传递默认值

[英]How to pass default for named parameters in javascript

I have a method in typescript as below using named parameters我在 typescript 中有一个使用命名参数的方法,如下所示

public foo({x, y, z , m , n} : {x:string, y: number, z: number, m?:string, n?:number}) {
}

parameters m and n will be provided from another object like参数mn将从另一个 object 提供,例如

const default = { m : 'M', n :10, o:6 }

Now I want to call foo like below and I want default parameters will be added without explicitly passing them in the call现在我想像下面这样调用 foo 并且我希望添加默认参数而不在调用中显式传递它们

  foo({x:'x', y: 5, z: 0})

So my question is how to apply default in the body of foo or somehow intercepting foo before call and apply default所以我的问题是如何在foo的主体中应用default ,或者在调用之前以某种方式拦截foo并应用default

public foo({x, y, z , m , n} = {x:string, y: number, z: number, m?:string, n?:number}) {
   // for example how to apply default here

}

Please note for simplicity I reduced number of parameters请注意,为简单起见,我减少了参数数量

Also I know these below solutions already I am looking for something with less boilerplate code我也知道以下这些解决方案我正在寻找样板代码更少的东西

public foo({x, y, z , m , n} = {x:string, y: number, z: number, m?:string, n?:number}) {
   if (!m) {
     m = default.m;
   }
   if (!n) {
     n = default.n;
   }

}

or或者

foo({...default, x:'x', y: 5, z: 0 });

For merge, u need to use merge using de-structure .对于合并,您需要使用de-structure进行合并。 default assignment will not work here. default分配在这里不起作用。 The default assignment is for work only if the object is undefined while passing value.仅当 object 在传递值时未定义时,默认分配才有效。 So you need to merge the default value with passed value.因此,您需要将默认值与传递的值合并。

Please check comment in code.请检查代码中的注释。

interface Foo {
  x: string;
  y: number;
  z: number;
  m?: string;
  n?: number;
  o?: number;
}
const defaultValue = { m: "M", n: 10, o: 6 } as Foo;
class A {
  public foo(props: Foo) {
    const { x, y, z, m, n } = { ...defaultValue, ...props };
    console.log(x, y, z, m, n);
  }
  public foo2({ x, y, z, m = defaultValue.m, n = defaultValue.n }: Foo) {
    // this will work, but verbose
    console.log(x, y, z, m, n);
  }
  public foo1({ x, y, z, m, n }: Foo = defaultValue) {
    // this will work only if foo1 called without argument
    console.log(x, y, z, m, n);
  }
  public print() {
    this.foo({ x: "x", y: 5, z: 0 }); // x 5 0 M 10
    this.foo1(); // undefined undefined undefined 'M' 10
    this.foo1({ x: "x", y: 5, z: 0 }); // x 5 0 undefined undefined
    this.foo2({ x: "x", y: 5, z: 0 }); // x 5 0 M 10
  }
}
const a = new A();
a.print();

foo and foo2 function will work. foofoo2 function 将工作。 However, foo2 is very verbose if more then few arguments.但是,如果 arguments 的数量过多,则 foo2 会非常冗长。 Use Object.assign() or {...} to merge value.使用Object.assign(){...}合并值。

How about combining and destructing object inside function?在 function 中组合和破坏 object 怎么样?

type someType = {x:string, y: number, z: number, m?:string, n?:number};
const initialValues = { m : 'M', n :10, o:6 }

function foo(obj: someType) {
  const {x, y, z , m , n} = {
    ...initialValues,
    ...obj
  }

}

You can just add a default value in the parameter itself like below:您可以在参数本身中添加一个默认值,如下所示:

public foo({x, y, z , m = 'a' , n = 10} = {x:string, y: number, z: number, m?:string, n?:number}) {
}

If you pass a value, default value will be overridden.如果您传递一个值,默认值将被覆盖。 With this way, you dont even need an if to check the presence of the value.通过这种方式,您甚至不需要if来检查值的存在。

and you can still call the method as:您仍然可以将该方法称为:

foo({...default, x:'x', y: 5, z: 0 });

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

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