亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

創建一個香蕉對象

創建一個香蕉對象

拉莫斯之舞 2023-10-14 15:52:46
最近遇到一個面向對象編程的問題,我想不出來。以下是說明:1. create a banana object2. all bananas are yellow3. all bananas have a length and diameter which are set at instatiation4. bananas also have isYummy property, which is set to true, because all bananas are yummy.5. all bananas have the ability to rot, which toggles isYummy to false.我真的不熟悉實例化長度和直徑以及函數的含義。這是我的方法。    const Banana = (color, length, diameter, isYummy) => {        this.color = 'yellow';        this.length = length;        this.diameter = diameter;        this.isYummy = true    }    let rot = new Banana('yellow', length, diameter, false)
查看完整描述

3 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

const Banana = (color, length, diameter, isYummy) => {

        this.color = 'yellow';

        this.length = length;

        this.diameter = diameter;

        this.isYummy = true

    }


    let rot = new Banana('yellow', length, diameter, false)

所有香蕉都是黃色的,所以你不需要參數中的顏色,而且 isYummy 最初設置為 true,所以你也不需要它。您唯一的輸入是長度和直徑。(好久沒寫JS了,語法不太記得了)


const Banana = (length, diameter) => {

        this.color = 'yellow';

        this.length = length;

        this.diameter = diameter;

        this.isYummy = true

    }


    let myBanana = new Banana(length, diameter)

香蕉也會腐爛,這是一種行動,而行動自然是方法。js中的實例方法是對象的原型。


在在線編譯器中,我對你的代碼有疑問,所以我像這樣重寫它:


如果 Banana 使用該編譯器中的函數編寫,則它可以是對象。


const Banana = function (length, diameter) {

        this.color = 'yellow';

        this.length = length;

        this.diameter = diameter;

        this.isYummy = true

    };

然后你需要向其添加一個操作(注意,如果你使用 => 箭頭函數,this上下文將會不同,而不是對象的上下文。所以要小心)


Banana.prototype.rot = function(){this.isYummy = false;}

現在從我們剛剛定義的對象創建我們的香蕉。


let myBanana = new Banana(20, 3);

console.log(myBanana.isYummy);

現在讓它腐爛:


myBanana.rot();

console.log(myBanana.isYummy);

const Banana = function (length, diameter) {

            this.color = 'yellow';

            this.length = length;

            this.diameter = diameter;

            this.isYummy = true

        };

    

Banana.prototype.rot = function(){this.isYummy = false;}


let myBanana = new Banana(20, 3);


console.log(myBanana.isYummy);


myBanana.rot();


console.log(myBanana.isYummy);


查看完整回答
反對 回復 2023-10-14
?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

您可以在構造函數中創建一個具有默認值的類,如下所示:


class Banana{

  constructor(length, diameter){    

    this.color = 'Yellow';

    this.length = length;

    this.diameter = diameter;

    this.isYummy = true; 

  }

}


var myBanana = new Banana('10cm', '2cm');

// to toggle rot or isYummy property

myBanana.isYummy = false;

console.log(myBanana) // to see your object in console


查看完整回答
反對 回復 2023-10-14
?
DIEA

TA貢獻1820條經驗 獲得超2個贊

目前尚不清楚您需要什么幫助,但以下是我注意到的一些問題:

  • 據我所知,箭頭函數不能是構造函數。嘗試實例化是Banana行不通的。

  • this.color并且在開始時this.isYummy總是設置為 true,因此不需要將相應的參數傳遞到構造函數中。

  • 我猜你誤解了這rot件事 - 它可能并不意味著是一個單獨的變量或屬性,而只是一些邏輯,例如將屬性設置isYummy為 false 的超時:

setTimeout(() => { this.isYummy = false }, 1000); // rot after one second

將其放在構造函數的末尾。


查看完整回答
反對 回復 2023-10-14
  • 3 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號