为什么使用 OOP 时字符串旁边有一个“未定义”?

我目前正在使用 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实例时有问题,所以我尝试更改一些语法但无济于事。


绝地无双
浏览 111回答 3
3回答

桃花长相依

您可以return像这样使用您的功能。因为您没有返回任何值。那西undefinedfunction Recipe(name, ingredients, price) {&nbsp; &nbsp; this.name = name;&nbsp; &nbsp; this.ingredients = ingredients;&nbsp; &nbsp; this.price = price;}function describe(name, ingredients, price) {&nbsp; &nbsp; return "<h2> Recipe name: " + name + "</h2> Ingredients: " + ingredients + "<br&nbsp; />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>&nbsp; &nbsp; <body>&nbsp; &nbsp; <p id = "p"></p></body></html>我已经删除document.write了return字符串。

天涯尽头无女友

问题是你在函数describe内部调用document.write函数。它写undefined因为 describe 什么都不返回。发生的事情是:首先,describe函数在文档中写入 html 文本。然后,您尝试describe在文档中编写函数的返回。您不需要将describe函数放在里面,document.write.只需使用您想要的参数调用它即可。

慕尼黑5688855

现在它的工作<html>&nbsp; &nbsp; <body>&nbsp; &nbsp; <p id = "p"></p><script>function Recipe(name, ingredients, price) {&nbsp; &nbsp; this.name = name;&nbsp; &nbsp; this.ingredients = ingredients;&nbsp; &nbsp; this.price = price;}function describe(name, ingredients, price) {&nbsp; &nbsp; document.write("<h2> Recipe name: " + name + "</h2> Ingredients: " + ingredients + "<br&nbsp; />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");//editeddescribe(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>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript