如何修复 eslint 错误 no-use-before-define
How to fix eslint error no-use-before-define
我正在使用 Airbnb 的风格指南,并且有几个函数相互引用,无论哪个函数在另一个之上都会触发此错误。除了禁用它之外,是否有关于如何修复此错误的最佳实践?
function foo() {
const ham = document.querySelector('#ham');
ham.onclick = () => {
bar();
};
}
function bar() {
const spam = document.querySelector('#spam');
spam.onclick = () => {
foo();
};
}
您可能只需要禁用第一个功能,因为您的用例也很受欢迎:
function foo() {
const ham = document.querySelector('#ham');
ham.onclick = () => {
/* eslint-disable no-use-before-define */
bar();
};
}
或者您可以将它们包装在文字对象中以解决问题:
const wrapper = {
foo() {
const ham = document.querySelector<HTMLDivElement>('#ham');
ham.onclick = () => {
wrapper.bar();
};
},
bar() {
const spam = document.querySelector<HTMLDivElement>('#spam');
spam.onclick = () => {
wrapper.foo();
};
}
};
我正在使用 Airbnb 的风格指南,并且有几个函数相互引用,无论哪个函数在另一个之上都会触发此错误。除了禁用它之外,是否有关于如何修复此错误的最佳实践?
function foo() {
const ham = document.querySelector('#ham');
ham.onclick = () => {
bar();
};
}
function bar() {
const spam = document.querySelector('#spam');
spam.onclick = () => {
foo();
};
}
您可能只需要禁用第一个功能,因为您的用例也很受欢迎:
function foo() {
const ham = document.querySelector('#ham');
ham.onclick = () => {
/* eslint-disable no-use-before-define */
bar();
};
}
或者您可以将它们包装在文字对象中以解决问题:
const wrapper = {
foo() {
const ham = document.querySelector<HTMLDivElement>('#ham');
ham.onclick = () => {
wrapper.bar();
};
},
bar() {
const spam = document.querySelector<HTMLDivElement>('#spam');
spam.onclick = () => {
wrapper.foo();
};
}
};