简体   繁体   English

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

[英]Function Object Literal Converted in ES6 Class

I am trying to convert a function that has an object literal inside of it to a class and I am not sure how I would handle the object literal when converting to a class. 我正在尝试将内部具有对象常量的函数转换为类,并且不确定转换为类时如何处理对象常量。 Example: 例:

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

So I am hoping to figure out how to do something like this: 所以我希望弄清楚如何做这样的事情:

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

Don't know how to handle the object literal? 不知道如何处理对象文字?

I am wanting to change the function to a class that will have a constructor for the channel and name, but not sure how to handle the object literal. 我想将函数更改为一个具有通道和名称的构造函数的类,但不确定如何处理对象文字。

Thank you for help. 谢谢你的帮助。

You would put exactly the same code that is currently in the ES5 constructor into the ES6 classes constructor: 您可以将与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