规则名 | 规则类型 | 深入了解 |
---|---|---|
getter-return |
problem |
The get syntax binds an object property to a function that will be called when that property is looked up. It was first introduced in ECMAScript 5:
var p = {
get name(){
return "nicholas";
}
};
Object.defineProperty(p, "age", {
get: function (){
return 17;
}
});
Note that every getter
is expected to return a value.
This rule enforces that a return statement is present in property getters.
此规则的 错误 代码实例:
/*eslint getter-return: "error"*/
p = {
get name(){
// no returns.
}
};
Object.defineProperty(p, "age", {
get: function (){
// no returns.
}
});
class P{
get name(){
// no returns.
}
}
此规则的 正确 代码实例:
::: correct
/*eslint getter-return: "error"*/
p = {
get name(){
return "nicholas";
}
};
Object.defineProperty(p, "age", {
get: function (){
return 18;
}
});
class P{
get name(){
return "nicholas";
}
}
This rule has an object option:
"allowImplicit": false
(default) disallows implicitly returningundefined
with areturn
statement.
选项 { "allowImplicit": true }
的 正确 代码示例:
::: correct
/*eslint getter-return: ["error", { allowImplicit: true }]*/
p = {
get name(){
return; // return undefined implicitly.
}
};
If your project will not be using ES5 property getters you do not need this rule.