第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么使用 OOP 時(shí)字符串旁邊有一個(gè)“未定義”?

為什么使用 OOP 時(shí)字符串旁邊有一個(gè)“未定義”?

絕地?zé)o雙 2022-11-03 15:17:10
我目前正在使用 OOP 來(lái)顯示烹飪食譜。一切都很好,除了我使用該document.write方法時(shí)。當(dāng)顯示price. 這是我的代碼:<html>    <body>    <p id = "p"></p><script>function Recipe(name, ingredients, price) {    this.name = name;    this.ingredients = ingredients;    this.price = price;}function describe(name, ingredients, price) {    document.write("<h2> Recipe name: " + name + "</h2> Ingredients: " + ingredients + "<br  />Price: " + price);}var instantRamen = new Recipe("Ramen", "Ramen noodles, hot water, salt, (optional) green pepper", "$2.00");var Bagel = new Recipe("Ham and cheese bagel", "Bagel (preferably an everything bagel), ham, cheese (of any type), pepper (just a little)", "$6.00");document.write(describe(instantRamen.name, instantRamen.ingredients, instantRamen.price));document.write(describe(Bagel.name, Bagel.ingredients, Bagel.price));</script></body></html>預(yù)期結(jié)果將類(lèi)似于“食譜名稱(chēng):拉面(換行)配料:拉面、熱水、鹽、(可選)青椒(換行)價(jià)格:2.00 美元”,但價(jià)格變?yōu)椤?.00 美元未定義”。其他一切都有效。我最初認(rèn)為創(chuàng)建instantRamenandBagel實(shí)例時(shí)有問(wèn)題,所以我嘗試更改一些語(yǔ)法但無(wú)濟(jì)于事。
查看完整描述

3 回答

?
桃花長(zhǎng)相依

TA貢獻(xiàn)1860條經(jīng)驗(yàn) 獲得超8個(gè)贊

您可以return像這樣使用您的功能。


因?yàn)槟鷽](méi)有返回任何值。那西undefined


function Recipe(name, ingredients, price) {

    this.name = name;

    this.ingredients = ingredients;

    this.price = price;

}


function describe(name, ingredients, price) {

    return "<h2> Recipe name: " + name + "</h2> Ingredients: " + ingredients + "<br  />Price: " + price;

}


var instantRamen = new Recipe("Ramen", "Ramen noodles, hot water, salt, (optional) green pepper", "$2.00");

var Bagel = new Recipe("Ham and cheese bagel", "Bagel (preferably an everything bagel), ham, cheese (of any type), pepper (just a little)", "$6.00");


document.write(describe(instantRamen.name, instantRamen.ingredients, instantRamen.price));

document.write(describe(Bagel.name, Bagel.ingredients, Bagel.price));

<html>

    <body>

    <p id = "p"></p>

</body>

</html>

我已經(jīng)刪除document.writereturn字符串。



查看完整回答
反對(duì) 回復(fù) 2022-11-03
?
天涯盡頭無(wú)女友

TA貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超9個(gè)贊

問(wèn)題是你在函數(shù)describe內(nèi)部調(diào)用document.write函數(shù)。它寫(xiě)undefined因?yàn)?describe 什么都不返回。

發(fā)生的事情是:首先,describe函數(shù)在文檔中寫(xiě)入 html 文本。然后,您嘗試describe在文檔中編寫(xiě)函數(shù)的返回。

您不需要將describe函數(shù)放在里面,document.write.只需使用您想要的參數(shù)調(diào)用它即可。


查看完整回答
反對(duì) 回復(fù) 2022-11-03
?
慕尼黑5688855

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超2個(gè)贊

現(xiàn)在它的工作


<html>

    <body>

    <p id = "p"></p>

<script>

function Recipe(name, ingredients, price) {

    this.name = name;

    this.ingredients = ingredients;

    this.price = price;

}


function describe(name, ingredients, price) {

    document.write("<h2> Recipe name: " + name + "</h2> Ingredients: " + ingredients + "<br  />Price: " + price );

}


var instantRamen = new Recipe("Ramen", "Ramen noodles, hot water, salt, (optional) green pepper", "$2.00");

var Bagel = new Recipe("Ham and cheese bagel", "Bagel (preferably an everything bagel), ham, cheese (of any type), pepper (just a little)", "$6.00");


//edited

describe(instantRamen.name, instantRamen.ingredients, instantRamen.price);

describe(Bagel.name, Bagel.ingredients, Bagel.price);

document.getElementById("p").innerHTML = "Your browser version is " + navigator.appVersion;

</script>

</body>

</html>


查看完整回答
反對(duì) 回復(fù) 2022-11-03
  • 3 回答
  • 0 關(guān)注
  • 106 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)