【问题标题】:How to initialize a const field in constructor in typescript?如何在打字稿的构造函数中初始化 const 字段?
【发布时间】:2016-05-22 06:35:58
【问题描述】:

我不知道这是否有价值。我只是好奇这是否可能?

问题:

如果可能的话,

的语法是什么

在 typescript 的构造函数中初始化一个 const 字段?`

export class Gulpfile {
  private dist: string;
  private src: string;

  constructor(){
    const this.dist = './dist/';
    this.src = './src'
  }
 }

【问题讨论】:

  • 你应该试试 Gulpfile.dist = './dist/'

标签: javascript class typescript constants


【解决方案1】:

你不能有一个常量类成员。

主要是因为类成员总是通过 this 关键字通过引用来引用,并且该引用始终可以更改。

ES6 也不支持。

【讨论】:

    猜你喜欢
    • 2010-11-28
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    相关资源
    最近更新 更多