如何在打字稿上传递无序参数?

时间:2019-01-25 13:48:40

标签: typescript

我有一个构造函数。我发送一些参数。如何传递类型变量而不更改其位置?

构造函数等待宽度变量,但是我想发送类型变量

constructor(
   name: string,
   field: string,
   width: number = 0,
   type: string = '',
   ) {
   this.name = name;
   this.field = field;
   this.width = width;
   this.type = type;
}

public static create(name: string, field: string, type?: string) {
    const scpCol = new ScpColumn(name, field, type); // constructor waits width variable but I want to send type variable
    return scpCol;
}

2 个答案:

答案 0 :(得分:2)

如果传递extension UIView { func fadeTransition(_ duration:CFTimeInterval) { let animation = CATransition() animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut) animation.type = CATransitionType.fade animation.duration = duration layer.add(animation, forKey: CATransitionType.fade.rawValue) } } func updateLabel() { myLabel.fadeTransition(0.4) myLabel.text = "Hello World" } ,则可以跳过默认初始化的参数并获取其默认值。

undefined

答案 1 :(得分:0)

如果我理解正确,则应按以下方式定义构造函数:

  constructor(
   name: string,
   field: string,
   type: string = '',
   width?: number = 0,

   )

并根据需要调用它