标签: default-value

ES6 默认参数为空对象?

我正在尝试为选项参数定义具有以下行为的构造函数:

class Test {
  constructor({
    someOption = 'foo',
    otherOption = 'bar',
    aSeedOfSomeSort = Math.random(),
    // ...
  }) {
    console.log(
      someOption, otherOption, aSeedOfSomeSort
    );
  }
}

new Test({someOption: 'lol'});
new Test({otherOption: 'derp'});
new Test({aSeedOfSomeSort: 0.5});
new Test({});
new Test(); // here's the problem
Run Code Online (Sandbox Code Playgroud)

这很好用,但是,我希望构造函数能够工作,以便使用所有默认参数,我不必传递空对象。我不关心对象本身是否在参数中命名,但在构造函数的上下文中,我想要一种干净的方法来直接访问所有选项,而无需命名空间或使用with.

这可能吗?

javascript constructor default-value ecmascript-6

0
推荐指数
1
解决办法
3842
查看次数

pid_t 是否有任何合理的初始/无效值?

打开-Wextra标志gcc似乎具有禁止structs部分初始化的效果。例如:

// main.c
#include <pthread.h>

typedef struct S {
  int i;
  pid_t pid;
} S;

int main( int argc, char* argv[] ) {
  (void)argc;
  (void)argv;
  S s = { 42 };
  (void)s;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)
$ gcc --version && gcc -Wall -Wextra -pedantic -Werror ./main.c
gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516
Copyright (C) 2016 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for …
Run Code Online (Sandbox Code Playgroud)

c gcc posix pid default-value

0
推荐指数
2
解决办法
183
查看次数

Scala:高阶函数中函数参数的默认值

我有高阶函数:

\n
def\xc2\xa0calculateFusionRiskContract(postCalc:\xc2\xa0DataFrame\xc2\xa0=>\xc2\xa0DataFrame) \n
Run Code Online (Sandbox Code Playgroud)\n

如何设置仅返回 postCalc 参数的 postCalc 参数的默认值?没有任何计算?

\n
def\xc2\xa0calculateFusionRiskContract(postCalc:\xc2\xa0DataFrame\xc2\xa0=>\xc2\xa0DataFrame = ???) \n
Run Code Online (Sandbox Code Playgroud)\n

scala default-value higher-order-functions

0
推荐指数
1
解决办法
71
查看次数

分配给原始 int 的 Null Integer 对象会抛出 NullPointerException

Integer i = null;
int j = i;
System.out.println(j);
Run Code Online (Sandbox Code Playgroud)

为什么它抛出NullPointerException并且不打印jas的值0

java default-value

-1
推荐指数
1
解决办法
6555
查看次数

C++ 函数中的默认值?

函数中的默认值到底是如何工作的?我的问题与这个例子有关:

int func(int number, std::string name = "None", int anotherNumber);

..

int func(int number, std::string name, int anotherNumber){
    ...
}

func(1, 2);
^Error, name is NULL yet we've defined a default value?
Run Code Online (Sandbox Code Playgroud)

编译器给出一个错误,抱怨参数为 NULL 并且不应该为 NULL。但我已经为其定义了默认值。

为什么是这样?

c++ default-value

-1
推荐指数
1
解决办法
3471
查看次数