为了账号安全,请及时绑定邮箱和手机立即绑定

为什么使用 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 关注
  • 110 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号