0

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

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;
        }
    };
}

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

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

谢谢你的帮助。

4

1 回答 1

2

您可以将当前在 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;
            }
        };
    }
}
于 2016-07-20T13:17:28.187 回答