javascript - 根据同一类的另一个成员设置 Typescript 类成员的值
问题描述
假设我有一堂课User
:
export class User {
constructor(
userID?: number,
state?: 'In' | 'Out' | 'Break' | 'Lunch', // for example, but there are more...
stateVal?: 1 | 2 | 3 | 4,
) {}
}
的目的stateVal
是使排序按state
特定顺序而不是字母顺序,即默认情况下它按字母顺序排序,这可能导致不希望的顺序->
(default, alphabetical)
(1) Break
(2) In
(3) Lunch
(4) Out
(desired order)
(1) In
(2) Break
(3) Lunch
(4) Out
这样,如果我排序 thisstateVal
而不是state
,我可以得到我想要的顺序。
总的来说,发生的事情是,我从 RESTful API 获取用户列表,然后我必须stateVal
通过循环每个用户并stateVal
手动添加来添加(因为stateVal
服务器不提供)。
但是这可以在课堂上完成吗?
我在想:
是否可以自动将state
属性stateVal
取决于自身的值?
IE
如果state
是“在”,自动设置stateVal
为 1——从类的角度来看?以某种方式强制执行此逻辑?
解决方案
推荐阅读
- typescript - 扩展多个接口的更好方法
- java - 在 Junit 5 中使用 Mockito 静态模拟时如何抛出 InterruptedException?
- apache - 子域HTTP和HTTPS去不同的目录,Apache2.4
- python - 如何将matplotlib图形转换为二进制类型
- python-3.x - 如何修复“列表索引超出范围错误”
- python - 在索引页上将数据从 c# 打印到烧瓶
- android - 如何在 Chrome、Android 上的 Video.js 中设置 HLS (m3u8) 的初始时间
- java - log4j2 spring boot 查找无法从 application.properties 读取属性
- xml - 使用函数“parse-json”进行转换会返回错误
- cypress - Cypress-cucumber - 如何将步骤存储在与功能相同的目录中