- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在我的程序中使用 const
关键字。
export class Constant {
let result : string;
private const CONSTANT = 'constant'; //Error: A class member cannot have the const keyword.
constructor () {}
public doSomething () {
if (condition is true) {
//do the needful
}
else
{
this.result = this.CONSTANT; // NO ERROR
}
}
}
问题1:为什么class member在typescript中没有const关键字?
问题2:当我使用
static readonly CONSTANT = 'constant';
并将其分配给
this.result = this.CONSTANT;
它显示错误。为什么会这样?
我已经关注了这篇文章How to implement class constants in typescript?但不知道为什么 typescript 使用 const
关键字显示这种错误。
最佳答案
Question1: why the class member does not have the const keyword in typescript?
按设计。除其他原因外,因为 EcmaScript6 doesn't either .
这里专门回答了这个问题:'const' keyword in TypeScript
Question2: When I use
static readonly CONSTANT = 'constant';
and assign it in
this.result = this.CONSTANT;
it displays error. why so?
如果您使用static
,那么您不能使用this
来引用您的变量,而是使用类的名称!
export class Constant{
let result : string;
static readonly CONSTANT = 'constant';
constructor(){}
public doSomething(){
if( condition is true){
//do the needful
}
else
{
this.result = Constant.CONSTANT;
}
}
}
为什么?因为this
指的是字段/方法所属类的实例。对于静态变量/方法,它不属于任何实例,而是属于类本身(快速简化)
关于typescript - 为什么我们不能在 TypeScript 类中定义一个 const 字段,为什么 static readonly 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46784333/
我有一个只读的文本框,但想回发它的值。之前我将代码设置为: /asp:TextBox> 它不工作。我删除了 ReadOnly="true" 并在代码中添加了以下行 txtContractStartDa
一些背景 因此,我正在完成一个 Web 应用程序的一些增强功能,该应用程序可以对数据进行一些自动插值。用户填写了一些文本框,它们之间的其他文本框具有自动计算的值。接收自动计算值的文本框必须是只读的,以
我刚刚审查了一些代码,注意到有人将成员标记为 readonly private。这与 private readonly 有任何不同吗? 例子: readonly private MyClass myI
我刚刚被告知 readonly ref readonly 现在是有效的 C#,我可以清楚地看到它作为面试问题的用途,但除此之外,它什么时候可以用于现实生活? 最佳答案 当您有一个可变结构时,您希望其中
当我使用以下命令运行我的Reactjs + ElectronJS应用程序时,它可以很好地运行: yarn start 。 当我尝试使用以下命令生成该错误时: yarn build 。 我不明白这是怎么
例子1 private const string _DefaultIconPath = _IconsPath + "default.ico"; private const string _IconsP
尝试访问 RecipeList.js 和 Recipe.js 的 .props 时出现语法错误。 这是 Recipe.js 的代码示例: import React, {Component} from
我是 Vue 新手。我正在尝试开发一个聊天应用程序,其中好友列表将显示在左侧菜单上,聊天框将显示在正文中。我正在使用 ajax 调用加载好友列表,并根据好友 ID 路由到聊天框。这是代码示例。
我是 SharePoint 的初学者开发人员,我一直使用 Google map 。我安装了 google-maps-react 但有一个错误告诉我。我不知道如何解决它...请让我知道这个解决方案...
我不确定我是否理解 IE 在我正在使用的脚本中的行为。这是我正在使用的脚本的一部分,它似乎在 Chrome 中运行良好 $(document).keydown(function (event) {
我对原生 react 完全陌生。 我有以下组件: 播放列表: export default class Playlists extends Component { playlists = [
所以我正在使用 MVC3 生成一个文本区域元素。我使用以下标记生成我的元素: model.Description, new { value=Model.Description, @class="re
我有一个从 php 文件创建的表单,以及一个具有只读属性的文本字段,在单击编辑按钮时(编辑按钮也在创建输入字段期间创建),我希望所述字段是可编辑,我该怎么做? value= readOnly>
我正在尝试输入组件的属性并同时使用 URL 参数。我收到以下错误: Property 'match' does not exist on type 'Readonly & Readonly' 这是我的
这个问题在这里已经有了答案: Array.IsReadOnly inconsistent depending on interface implementation (4 个答案) 关闭 8 年前。
根据文档 ( here ),Google 提供了一些相同的范围: https://www.googleapis.com/auth/calendar读/写访问到日历 https://www.google
我使用的是JAWS v2023.2212.23,我有一个div容器,里面有段落和列表。我的要求是,当我专注于div容器时,JAWS应该读取整个块。。对于演示角色,它将只读第一段。。谁能帮我理解所有咏叹
我有一个单选按钮控件,其中有两个项目。 Add A Delete D 我想在条件 X 上仅禁
一、Reactive api 下面我们可以一个例子 如图所示,当我们点击button按钮的时候,是没有响应式效果的,此时我们需要reactive api。 那么这是什么原因呢?为什么就可以变成响应式的
我正在寻找一种在ng-readonly语句中调节ng-if的方法..我尝试了几种方法,这是我的最新建议。 ng-if="note.owner_image === user.image " ng-re
我是一名优秀的程序员,十分优秀!