简体   繁体   English

如何修复 eslint 错误 no-use-before-define

[英]How to fix eslint error no-use-before-define

I am using Airbnb's style guide and have a couple of functions that reference each other this error is triggered no matter which function is above the other.我正在使用 Airbnb 的样式指南,并且有几个相互引用的函数,无论哪个 function 高于另一个,都会触发此错误。 Is there a best practice on how to fix this error besides just disable it?除了禁用它之外,是否有关于如何修复此错误的最佳实践?

function foo() {
    const ham = document.querySelector('#ham');
    ham.onclick = () => {
        bar();
    };
}

function bar() {
    const spam = document.querySelector('#spam');
    spam.onclick = () => {
        foo();
    };
}

You might have to just disable for the first function since your use case is also popular:您可能只需要禁用第一个 function 因为您的用例也很受欢迎:

function foo() {
    const ham = document.querySelector('#ham');
    ham.onclick = () => {
        /* eslint-disable no-use-before-define */
        bar();
    };
}

Or you can wrap them up in an literal object to resolve the issue:或者您可以将它们包装在文字 object 中以解决问题:

const wrapper = {
  foo() {
    const ham = document.querySelector<HTMLDivElement>('#ham');
    ham.onclick = () => {
        wrapper.bar();
    };
  },

  bar() {
    const spam = document.querySelector<HTMLDivElement>('#spam');
    spam.onclick = () => {
        wrapper.foo();
    };
  }
};

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

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