func-style
强制一致地使用 function
声明或表达式
在 JavaScript 中有两种定义函数的方式:function
声明和 function
表达式。声明首先包含 function
关键字,然后是名称,然后是其参数和函数体,例如:
function doSomething() {
// ...
}
等效函数表达式以 var
关键字开头,后跟名称,然后是函数本身,例如:
var doSomething = function() {
// ...
};
function
声明和 function expressions
之间的主要区别在于声明被提升到定义它们的范围的顶部,这允许您编写在声明之前使用函数的代码。例如:
doSomething();
function doSomething() {
// ...
}
尽管这段代码看起来像是一个错误,但实际上它运行良好,因为 JavaScript 引擎将 function
声明提升到了作用域的顶部。这意味着此代码被视为声明出现在调用之前。
Q9K/ZCm3gnrrsLgl6+Vg1zYNVm15HH1PJKp7JDTWxnI3UGw75QnoXn7NUlKZR+VcO1ZDAnEM523QUHBFGmrY48L1OgY+X6rJkn775G5BKDwVfJU9hnhhclJemuwhHUdX0G+dAIE4bZ5lvdJAe3WXew==
doSomething(); // error!
var doSomething = function() {
// ...
};
qofccF0nJMe/kcbJPdlNwehDizjRX1y0gSrjNt/7HIO6dVcnUMVL2l63TPSbSp4Hti0dYyWUaBTWlRNTspkrynPaEf59ocBtBDudVx7IqluLV9ecraE6k+5HBFk4BiEHXrxJxlgM390Z0N12rOqYQg==
SruSh29PtMuDmFe+K0sec/zv2maMYZKId2LZPDnHj/psf14WLCoOKMN5ejgh682MG7AYANRwoWAV0Nw1quwi0Lv/o5XtK6C8DPNOD1IAefkw/593GuWgtxRiaYGfmZlLZJIQdwhum8u09SSXxhyhQqosgZYNq3ypw7zvTlFsBOsQksb4qXjqhB5DIxCoNcJKfAzjsp/+LyZCiX6HyrV6Bw==
规则详情
9zTh0RKdRwbmPwM4NqucZFqLRbKsONsW4zdZOy1vZdEloPUyaFJpV7ML0t7urBBFq2NMrfx1TDLodLBXsicorTW0qvFfTgj6SGd3D+m2cVbJR9EWqNDpzJIiWr9pkEfIz0mL62FD8kq5zPOOaxyZlAr3X7F2QfVhs+QiCLcV7QR4sgmsMxiuUijLBSb5IeR0uJLJQumrxRl+BaSxAKyZ8lxt0sYeQHq7OtTfsM3Ghsw=
选项
iUBjM40SPBc84UHpTbhTXsqjfMLBa6laeAzGXZqZpBVXyz5IauXehqHLkVOssi9B
- 6C6tIIjKtIJiwvYueppHQgdCRKWIMrNj85OU4LbdN4dXu9UBrOp5lygFxAnlCauE8F4VNwo1DLA62ShmuFcwC2VYdb3Sfl4wDaB2VSO46tItKmGUWRF9DGhCfDZ6K87djAHi7sYH9rM6ksD6JN6Oi4WauTJCIBb1/XQHNrwcwYOReri8YjZhjQwWoK1SGz6Mz+Brb/rJDznM41rdjIxp5nM5FDBg694Fr8FNFlDWsb/tvZIuKhQwISzJKG47UUF+
iUBjM40SPBc84UHpTbhTXul3XTfaSGMYlLaig8Grak2t6gijeJqDvUaqlNNKEN7v
- hAjk05IEKh++biF/2hMM3YbyCKAndLMVKAy5fmf1MaRgv68S+mThg4AEXmgJpScjsFoEeNuDNiNh7LLNc3jM/gMs+zTN7Q0pOA2DWGnYfppVKmFWr/yC2C2/4HubOobv/txV+Nur9fummd5iBFTluf/a9xoZFEkW6ZK3/loesJUaImoVibhqAt55dWIXYhIWefyAQXYYqXT+oxSTkFhHACMDnLjbi1kOJslAu2pGXYMidzOXhephSGvS4NxXQvY1gynt/BPpJOveXa0qjxYLmWmUIAWAV43J9hP2uQYK1RmkjHDbSkafQWgeObOCfoNtcvMTIm760c1QmGXXdN83e7co1FuR/UgqSUSO5fxmw7pXKMRrLcYse+YXdZDibs8OZNWB2hUB04mxUb/lQB0xhNZBKRokX7dDmfJzggMsaH4=
expression
UC6gNaibYrBNtz3KfP9Eeu/4mQyCqSop/0kgqS9bPsviySJNnws4Ba8tZ/WVDB+0MuZJhX+wUwCZpcKVE8M8yuAaIueaF/IQMd5dGpCLRCY=
/*eslint func-style: ["error", "expression"]*/
function foo() {
// ...
}
UC6gNaibYrBNtz3KfP9Eeu/4mQyCqSop/0kgqS9bPsviySJNnws4Ba8tZ/WVDB+0giAs/nY3lwFLgd51YOH3+MV1VxgQJnSuN0hdwTEk/mA=
/*eslint func-style: ["error", "expression"]*/
var foo = function() {
// ...
};
var foo = () => {};
// allowed as allowArrowFunctions : false is applied only for declaration
declaration
zsV3VIEaArmSNnLhUR0dD+nsGuBwMpIRVENv5JLg7cyYYPWVbnc1giY3O0YRrn4pWq04hWpqR5D35ScOELr2y2FjcuMsNZJFd81iNMek+xw=
/*eslint func-style: ["error", "declaration"]*/
var foo = function() {
// ...
};
var foo = () => {};
zsV3VIEaArmSNnLhUR0dD+nsGuBwMpIRVENv5JLg7cyYYPWVbnc1giY3O0YRrn4p1yRU/mXeizovECCCJ0GXPXaBl7kqnSO01XIBCitGH1A=
/*eslint func-style: ["error", "declaration"]*/
function foo() {
// ...
}
// Methods (functions assigned to objects) are not checked by this rule
SomeObject.foo = function() {
// ...
};
allowArrowFunctions
zsV3VIEaArmSNnLhUR0dD+nsGuBwMpIRVENv5JLg7cz6BLwiRcukUiF2n4o1RuG6mL5+YZhLOLsOvKcRSnKdsuO/lhVpoK9MBiAXVVK+JfDheFFRSEo++elSeqIIvpdwzF2ed3wLdFfVTHqPbD8PSLCBLXlrwXUJfeHRTM6dVxg=
/*eslint func-style: ["error", "declaration", { "allowArrowFunctions": true }]*/
var foo = () => {};
何时不使用
hiRyZiBBwFjNBOJmKNvs5dkwB9iwvaRnJJoTvjna9vAsOGAUrCp8W8HyidyqKcKKjbMWrpd/ZDmdSmfNWjh3sTXdfdh0CuzrtnM62xOAjZ1Nf4JztouRPakKJQqppDRn9b6JwuZWPN4AC2wOCQOUPw==