Getter or setter functions should avoid an infinite call by accessing themselves

  • GETTER_SETTER_RECURSION
  • Error
  • High
  • No tags

This rule applies when getter or setter functions cause an infinite call by accessing themselves.

When a getter or setter function accesses itself (recursion), an exception by an infinite call is thrown.

Noncompliant Code Example

View with compliant examples side by side
let obj = {
    set name(n) {
        this.name = n; // GETTER_SETTER_RECURSION alarm because 'obj.name = "John"' causes an infinite call.
    },
    get name() {
        return this.name; // GETTER_SETTER_RECURSION alarm
    }
}
obj.name = "John";

Compliant Code Example

View with noncompliant examples side by side
let obj = {
    set name(n) {
        this._name = n;
    },
    get name() {
        return this._name;
    }
}
obj.name = "John";

Version

This rule was introduced in DeepScan 1.11.0-beta.

See

Was this documentation helpful?