在 ES6 中转换的函数对象文字 Class

Function Object Literal Converted in ES6 Class

我正在尝试将其中包含对象文字的函数转换为 class,但我不确定在转换为 class 时如何处理对象文字。示例:

function Commercial(channel, name) {
    this.recording = {
        isChannelLive: true,
        isNameRated: false,
        timeSlots: function() {
            this.active = false;
            this.recording = false;
        }
    };
}

所以我希望弄清楚如何做这样的事情:

class Commercial {
    constructor(channel, name) {
      this.channel = channel;
      this.name = name;
    }
    this.recording = {
        isChannelLive: true,
        isNameRated: false,
        timeSlots: function() {
            this.active = false;
            this.recording = false;
        }
    };
}

不知道如何处理对象字面量?

我想将函数更改为 class,它将具有通道和名称的构造函数,但不确定如何处理对象文字。

感谢您的帮助。

您可以将当前在 ES5 构造函数中的完全相同的代码放入 ES6 类 构造函数中:

class Commercial {
    constructor(channel, name) {
        this.channel = channel;
        this.name = name;
        this.recording = {
            isChannelLive: true,
            isNameRated: false,
            timeSlots: function() {
                this.active = false;
                this.recording = false;
            }
        };
    }
}