eslint/func-names 樣式
作用
要求或禁止具名函式表達式。
為什麼這不好?
如果函式沒有名稱,則會在其中或在其中呼叫的任何函式中拋出的錯誤的堆疊追蹤中顯示 <anonymous>
。這使得很難找到錯誤拋出的位置。如果為函式表達式提供可選名稱,則會在堆疊追蹤中取得函式表達式的名稱。
設定
此規則具有字串選項
"always"
要求函式表達式在任何情況下都必須有名稱。"as-needed"
要求函式表達式僅在執行階段不會自動推斷名稱時才需要有名稱。"never"
要求函式表達式在任何情況下都不得有名稱。
範例
此規則的不正確程式碼範例
javascript
/*oxlint func-names: "error" */
// default is "always" and there is an anonymous function
Foo.prototype.bar = function () {};
/*oxlint func-names: ["error", "always"] */
// there is an anonymous function
Foo.prototype.bar = function () {};
/*oxlint func-names: ["error", "as-needed"] */
// there is an anonymous function
// where the name isn’t assigned automatically per the ECMAScript specs
Foo.prototype.bar = function () {};
/*oxlint func-names: ["error", "never"] */
// there is a named function
Foo.prototype.bar = function bar() {};
此規則的正確程式碼範例
javascript
/*oxlint func-names: "error" */
Foo.prototype.bar = function bar() {};
/*oxlint func-names: ["error", "always"] */
Foo.prototype.bar = function bar() {};
/*oxlint func-names: ["error", "as-needed"] */
var foo = function () {};
/*oxlint func-names: ["error", "never"] */
Foo.prototype.bar = function () {};