简体   繁体   English

教程中此函数中的if-else语句如何有意义?

[英]How do the if-else statements in this function from a tutorial make any sense?

I'm going through this tutorial for a simple playlist with Vue. 我将通过本教程来学习Vue的简单播放列表。

I'm confused by the author's play() function, particularly the series of if-else statements. 我对作者的play()函数感到困惑,特别是一系列if-else语句。 It appears to check whether index is a number, and if it is (and I can't imagine how it wouldn't be), it seems to skip the rest of the code that checks for whether the selected track is the same as the current track, among other things (link to the repo and function in question here) : 似乎要检查index是否为数字,如果为数字(我无法想象它不会是数字),则似乎跳过了其余代码,以检查所选轨道是否与数字相同。当前曲目,以及其他内容(此处链接到所讨论的repo和函数)

play (index) {
  let selectedTrackIndex = this.playlist.findIndex(track => track === this.selectedTrack)

  if (typeof index === 'number') {
    index = index

  } else if (this.selectedTrack) {
    if (this.selectedTrack != this.currentTrack) {
      this.stop()
    }
    index = selectedTrackIndex
  } else {
    index = this.index
  }

  let track = this.playlist[index].howl

  if (track.playing()) {
    return
  } else {
    track.play()
  }

  this.selectedTrack = this.playlist[index]
  this.playing = true
  this.index = index
}

The author's summary of the above code: 上面代码的作者摘要:

The method takes an index as the parameter, which specifies the track to be played. 该方法以索引作为参数,该索引指定要播放的曲目。 First, we get the index of the selected track. 首先,我们获得所选曲目的索引。 Then, we make some checks to determine the value of the index. 然后,我们进行一些检查以确定索引的值。 If an index is provided as an argument and it's a number, then we use it. 如果提供索引作为参数,并且它是一个数字,那么我们将使用它。 If a track is selected, we use the index of the selected track. 如果选择了轨道,我们将使用选定轨道的索引。 If the selected track is different from the current one, we use the stop() method to stop the current one. 如果所选轨道与当前轨道不同,则使用stop()方法停止当前轨道。 Finally, if neither an index argument is passed nor a track is selected, we use the value of the index data property. 最后,如果既未传递索引参数也未选择轨道,则将使用索引数据属性的值。

I checked MDN and a few other sources and didn't see anything special about an if() followed by an else if() in JavaScript. 我检查了MDN和其他一些资源,但在JavaScript中没有发现关于if()else if()任何特殊之处。 So I figure I have to be missing something here. 所以我想我必须在这里丢失一些东西。 Why is a validation check on index tied to checking whether the selected track is the same as the current track? 为什么对index进行验证检查与检查所选轨道是否与当前轨道相同? The other checks inside the else if() and else blocks seem like stuff you wouldn't want to leave out, and yet his live demo of the app works. else if()else块中的其他检查似乎是您不希望遗漏的东西,但是他的应用程序实时演示仍然有效。 I asked the author to elaborate in the comments of the article but the article is almost a year old, so I'm not sure if he'll get back to me. 我要求作者在文章的评论中进行详细说明,但该文章已经发表了将近一年,所以我不确定他是否会回复我。 Thanks to everyone in advance. 预先感谢大家。

It's not really a validation test, it's just detecting whether an argument was supplied or not. 这实际上不是一个验证测试,它只是检测是否提供了参数。 If the argument isn't supplied, or it's not a number, the function performs default behaviors. 如果未提供参数,或者该参数不是数字,则该函数将执行默认行为。

If the caller uses track.play(2) , they're specifically telling it to play track 2. The test if (typeof index == 'number') will succeed, and we'll use that given index. 如果调用者使用track.play(2) ,他们专门告诉它播放轨道2。测试if (typeof index == 'number')是否成功,我们将使用给定的索引。

If the caller uses track.play() , index will be undefined , which isn't a number, so that test will fail, and then we'll continue with the else if test. 如果调用者使用track.play() ,则index将是undefined ,而不是数字,因此测试将失败,然后继续进行else if test。 If there's a selected track, we use that (first stopping the current track if it's different). 如果有选定的轨道,我们将使用该轨道(如果当前轨道不同,则首先停止它)。

If there's no selected track, we fall through to the final else clause, which uses the index property of the current object. 如果没有选定的轨道,我们将进入最后的else子句,该子句使用当前对象的index属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM