使用 javascript 函数时的问题(对象未按预期显示)

时间:2021-06-15 10:40:08

标签: javascript

我的目的是先运行 play() 函数,然后是 pause() 函数。 最后一个代码 document.write("</br>You should really pass on the second"); 只是为了测试所有代码是否运行(如果显示,代码运行正常,对吗?)

<!DOCTYPE html>
<html>
<head>
<title>c5vd4</title>
<meta charset="utf-8">
<script type="text/javascript">
var song = {
    name: "Walk This Way",
    artist: "Run-D.M.C.",
    minutes: 4,
    seconds: 3,
    genre: "80s",
    playing: false,
    play function() {
    if (!this.playing) {
    this.playing = true;
    document.write("Playing "+ this.name + " by " + this.artist);
        }
    },
    pause function() {
    if (this.playing) {
    this.playing = false;
    }
    }
}    
song.play();
song.pause();
document.write("</br>You should really pass on the second");
  </script>
  </head>
  </html>

但是,当我运行我的代码时,没有任何显示。甚至最后的代码。 你能给我一些关于这个问题的想法吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

您需要将 : 与暂停和播放一起使用:

var song = {
  name: "Walk This Way",
  artist: "Run-D.M.C.",
  minutes: 4,
  seconds: 3,
  genre: "80s",
  playing: false,
  play: function() {
    if (!this.playing) {
      this.playing = true;
      document.write("Playing " + this.name + " by " + this.artist);
    }
  },
  pause: function() {
    if (this.playing) {
      this.playing = false;
    }
  }
}
song.play();
song.pause();
document.write("</br>You should really pass on the second");