簡體   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