简体   繁体   English

使用“未使用的变量”

[英]Used “unused var”

ESLint is giving me the following error message: "'semitoneInterval' is assigned a value but never used [no-unused-vars]". ESLint 给了我以下错误消息:“'semitoneInterval' 被分配了一个值,但从未使用过 [no-unused-vars]”。 I guess it is pretty clear it isn't a unused var as I used it 30ish times.我想很明显它不是一个未使用的变量,因为我使用了 30 次。 Using only one return semitoneInterval;仅使用一个return semitoneInterval; in the end of the switch fixes the error, but I don't want to use this syntax due to the big amount of extra lines writing break;在 switch 的最后修复了错误,但我不想使用这种语法,因为有大量的额外行写break; . .

import React, { Component } from 'react';

export class Piano extends Component {
  getSemitoneIntervals = chordType => {
    let semitoneInterval;

    switch (chordType) {
      case '5': return semitoneInterval = [0, 7];
      case '': return semitoneInterval = [0, 4, 7];
      case 'm': return semitoneInterval = [0, 3, 7];
      case 'sus2': return semitoneInterval = [0, 5, 7];
      case 'sus4': return semitoneInterval = [0, 2, 7];
      case 'dim': return semitoneInterval = [0, 3, 6];
      case 'aug': return semitoneInterval = [0, 4, 8];
      case '7': return semitoneInterval = [0, 4, 7, 10];
      case 'm7': return semitoneInterval = [0, 3, 7, 10];
      case 'maj7': return semitoneInterval = [0, 4, 7, 11];
      case 'mM7': return semitoneInterval = [0, 3, 7, 11];
      case '6': return semitoneInterval = [0, 4, 7, 9];
      case 'm6': return semitoneInterval = [0, 3, 7, 9];
      case 'add2': return semitoneInterval = [0, 2, 4, 7];
      case 'add9': return semitoneInterval = [0, 4, 7, 14];
      case '7-5': return semitoneInterval = [0, 4, 6, 10];
      case '7+5': return semitoneInterval = [0, 4, 8, 10];
      case 'dim7': return semitoneInterval = [0, 3, 6, 9];
      case 'm7b5': return semitoneInterval = [0, 3, 6, 10];
      case 'aug7': return semitoneInterval = [0, 4, 8, 10];
      case '6/9': return semitoneInterval = [0, 4, 7, 9, 14];
      case '9': return semitoneInterval = [0, 4, 7, 10, 14];
      case 'm9': return semitoneInterval = [0, 3, 7, 10, 14];
      case 'maj9': return semitoneInterval = [0, 4, 7, 11, 14];
      case '11': return semitoneInterval = [0, 4, 7, 10, 14, 17];
      case 'm11': return semitoneInterval = [0, 3, 7, 10, 14, 17];
      case 'maj13': return semitoneInterval = [0, 4, 7, 11, 14, 21];
      case '13': return semitoneInterval = [0, 4, 7, 10, 14, 17, 21];
      case 'm13': return semitoneInterval = [0, 3, 7, 10, 14, 17, 21];
      default: return console.log('Not valid chord type');
    }
  }

  //[..]
}

export default Piano;

No, you are not use this variable, you just return it during the assignment, but you are not really use it.不,你没有使用这个变量,你只是在赋值期间返回它,但你并没有真正使用它。
you can just return the value without the assignment to the variable你可以只返回值而不给变量赋值

you are assigning semitoneInterval to a value and right away returning its value.您正在将semitoneInterval分配给一个值并立即返回其值。

Any return in your switch statement would cause the function to stop its execution and return a value. switch 语句中的任何返回都会导致 function 停止执行并返回一个值。 Therefore semitoneInterval is assigned a value in any of the case it goes through, but the value stored inside is never actually used (only the value returned by your function is).因此semitoneInterval在它经历的任何case都被分配了一个值,但其中存储的值从未实际使用过(只有 function 返回的值是)。

Your switch statement could work equally like this:您的 switch 语句可以像这样同样工作:

switch (chordType) {
      case '5': return [0, 7];
      case '': return [0, 4, 7];
    ...

this other eslint rule (although a bit of a different topic) explains as well (in better words) why assigning a value this way has no real use in your code.这个其他的eslint 规则(虽然有点不同的主题)也解释了(用更好的话说)为什么以这种方式分配一个值在你的代码中没有真正的用处。

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

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