您可以在数组内声明变量吗?

时间:2019-11-16 19:36:58

标签: typescript

我想创建一个每周的数组,每个数组属于我的“工作时间”课程。

当我尝试在数组中声明它们时,出现错误,并且在查看JS文件时看到数组已转换为const week = [let, sunday = new WorkHours,

有没有办法解决这个问题?

class WorkHours {
    day: string;
    night: string;
}

    let sunday = new WorkHours;
    let monday = new WorkHours;
    let tuesday = new WorkHours;
    let wednesday = new WorkHours;
    let thursday = new WorkHours;
    let friday = new WorkHours;
    let saturday = new WorkHours;

const week = [
    sunday,
    monday,
    tuesday,
    wednesday,
    thursday,
    friday,
    saturday,
    ]

1 个答案:

答案 0 :(得分:0)

您不能在数组内声明变量。您可以使用解构来缩小现有内容:

class WorkHours {
    day: string
    night: string
}

const week = [new WorkHours(), new WorkHours(), new WorkHours(), new WorkHours(), new WorkHours(), new WorkHours(), new WorkHours()]

const [sunday, monday, tuesday, wednesday, thursday, friday, saturday ] = week