gpt4 book ai didi

javascript - 更新我的对象时遇到问题

转载 作者:搜寻专家 更新时间:2023-11-01 04:12:46 25 4
gpt4 key购买 nike

您好,我是 JavaScript 的新手,现在有点卡住了。我正在做的项目的重点是创建一个卡片对象并具有设置新面孔和花色的方法。当我单击按钮以显示我的卡时,它可以工作,但是当我单击按钮以更新我的面部和西装并重新单击按钮以显示我的卡时,它不会显示更新的信息。我已经研究了很长时间了,但仍然不明白为什么它不起作用。请帮助:)

<!DOCTYPE html>
<html>

<head>
<title>Object Oriented Programming</title>
<script>
function Card(suit, face) {
this.suit = suit;
this.face = face;
this.card = face + ' of ' + suit;
this.showCard = function() {
alert(this.card);
}
this.setSF = function(newSuit, newFace) {
this.suit = newSuit;
this.face = newFace;
}
}

var card1 = new Card("Diamonds", 8);
</script>
</head>

<body>
<input type="button" value="Show card" onclick="card1.showCard()" />
<input type="button" value="Click to change the suit and face" onclick="card1.setSF('Hearts',5)" />

</body>

</html>

最佳答案

你很接近。这是因为您在设置新卡时没有更新 this.card。因此,不是提醒 this.card,而是提醒 this.face + ' of ' + this.suit 你更新了,然后删除 this.card 全部放在一起,因为它实际上没有做任何事情而且是多余的。

<!DOCTYPE html>
<html>

<head>
<title>Object Oriented Programming</title>
<script>
function Card(suit, face) {
this.suit = suit;
this.face = face;
this.showCard = function() {
alert(this.face + ' of ' + this.suit);
}
this.setSF = function(newSuit, newFace) {
this.suit = newSuit;
this.face = newFace;
}
}

var card1 = new Card("Diamonds", 8);
</script>
</head>

<body>
<input type="button" value="Show card" onclick="card1.showCard()" />
<input type="button" value="Click to change the suit and face" onclick="card1.setSF('Hearts',5)" />

</body>

</html>

关于javascript - 更新我的对象时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50901721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com