将可变数量的参数传递给JavaScript构造函数

时间:2018-05-09 15:21:29

标签: javascript typescript

在JavaScript构造函数下面,

function Person(){
    this.name = arguments[0];
    this.age = arguments[1];
}

let a: Person = new Person('Name1');
let b: Person = new Person('Name2', 20);

let d1: Date = new Date(0); // this works
let d2: Date = new Date(2012, 11); // this works

使用arguments对象接收可变数量的参数。

tsc编译器说:

# tsc file.ts
file.ts(6,9): error TS2350: Only a void function can be called with the 'new' keyword.
file.ts(6,9): error TS2554: Expected 0 arguments, but got 1.
file.ts(7,9): error TS2350: Only a void function can be called with the 'new' keyword.
file.ts(7,9): error TS2554: Expected 0 arguments, but got 2

如何将可变数量的参数传递给JavaScript构造函数?

0 个答案:

没有答案