gpt4 book ai didi

javascript - ES6 类中转换的函数对象字面量

转载 作者:行者123 更新时间:2023-12-02 14:23:36 25 4
gpt4 key购买 nike

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

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

不知道如何处理对象文字?

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

感谢您的帮助。

最佳答案

您可以将当前 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;
}
};
}
}

关于javascript - ES6 类中转换的函数对象字面量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38482318/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com