我目前正在使用 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实例时有问题,所以我尝试更改一些语法但无济于事。
桃花长相依
天涯尽头无女友
慕尼黑5688855
相关分类