繁体   English   中英

ES6类中转换的功能对象文字

[英]Function Object Literal Converted in ES6 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;
        }
    };
}

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

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

谢谢你的帮助。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM