We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
函数 7.1
function foo() {}
// 函数调用出现在函数定义之前,当代码行数比较多时,定位函数比较不方便,不利于从上到下阅读代码的习惯 doSomething(); function doSomething() { // ... } // 推荐如下形式 const doSomething = function() { // ... }; doSomething();
// doSomething.js // 报错信息只会定位到这个文件, 不会提示名称 export default () => { console.log('匿名函数'); throw new Error('匿名函数'); } // main.js import doSomething from './doSomething'; doSomething();
// 命名函数报错 export default function namedFunction() { console.log('命名函数'); throw new Error('命名函数'); }
The text was updated successfully, but these errors were encountered:
Sorry, something went wrong.
简写的函数式组件也经常会出现这种问题, 如
import { useEffect } from "react" export default () => { useEffect(() => { throw new Error('some error'); }, []); return <div> </div> }
会导致
// better 简洁一些 export function SomeComponent() { useEffect(() => { throw new Error('some error'); }, []); return <div></div>; } // best const SomeComponent = () => { useEffect(() => { throw new Error('some error'); }, []); return <div></div>; } export default SomeComponent;
命名函数在报错提示里会提示函数名称, 方便搜索
No branches or pull requests
函数 7.1
1. 使用
function foo() {}
形式,导致作用域提升,会出现在函数被声明之前就可以使用1. 推荐使用命名函数,而不是匿名函数,在函数体报错时方便追踪
The text was updated successfully, but these errors were encountered: