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

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

為什么使用 OOP 時字符串旁邊有一個“未定義”?

為什么使用 OOP 時字符串旁邊有一個“未定義”?

絕地無雙 2022-11-03 15:17:10
我目前正在使用 OOP 來顯示烹飪食譜。一切都很好,除了我使用該document.write方法時。當顯示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>預期結果將類似于“食譜名稱:拉面(換行)配料:拉面、熱水、鹽、(可選)青椒(換行)價格:2.00 美元”,但價格變為“2.00 美元未定義”。其他一切都有效。我最初認為創建instantRamenandBagel實例時有問題,所以我嘗試更改一些語法但無濟于事。
查看完整描述

3 回答

?
桃花長相依

TA貢獻1860條經驗 獲得超8個贊

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


因為您沒有返回任何值。那西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>

我已經刪除document.writereturn字符串。



查看完整回答
反對 回復 2022-11-03
?
天涯盡頭無女友

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

問題是你在函數describe內部調用document.write函數。它寫undefined因為 describe 什么都不返回。

發生的事情是:首先,describe函數在文檔中寫入 html 文本。然后,您嘗試describe在文檔中編寫函數的返回。

您不需要將describe函數放在里面,document.write.只需使用您想要的參數調用它即可。


查看完整回答
反對 回復 2022-11-03
?
慕尼黑5688855

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

現在它的工作


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


查看完整回答
反對 回復 2022-11-03
  • 3 回答
  • 0 關注
  • 109 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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