【问题标题】:Parameter property declaration with open ended constructor arguments带有开放式构造函数参数的参数属性声明
【发布时间】:2013-01-24 23:30:18
【问题描述】:

这可能吗?

我目前拥有的和有效的:

module app.typescriptdemoview {
    export class InitializeAutoComplete {
        constructor(... _inputIds: string[]) {
        // Initialize _inputIds as a member variable ..
        }
    }
}

我想要达到的目标:

module app.typescriptdemoview {
    export class InitializeAutoComplete {
        constructor(private ... _inputIds: string[]) { // Automatic member var. initialization
        }
    }
}

【问题讨论】:

  • 我不确定我是否理解您的问题,但如果您删除了 ... 部分,这将起作用。
  • @JohnnyHK 在构造函数参数前添加作用域关键字会自动将其初始化为类成员变量,而无需手动编写(例如) this._inputIds = _inputIds;我想将该功能与开放式构造函数参数(... 语法)结合使用。
  • 啊,现在我明白你要做什么了;很抱歉很厚。不幸的是,这似乎是不可能的。

标签: javascript typescript


【解决方案1】:

目前不可能,因此您只能手动分配它。编译器需要进行更改才能自动执行此操作。

class Test {
    private _args: string[];
    constructor (...args: string[]) {
        this._args = args;
    }
}

【讨论】:

    猜你喜欢
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-05
    • 1970-01-01
    • 2012-02-14
    相关资源
    最近更新 更多