# class-prototype

Rule `sonarjs/class-prototype`. Class methods should be used instead of "prototype" assignments

- Source: sonarjs
- Technologies: JavaScript
- Kind: style
- Needs type information: yes
- Autofix: no
- Reach: 0 weekly downloads of presets that enable it
- Trend: 0% last quarter
- Rank: #2123 by reach

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `sonarjs/class-prototype` | no | no | package `eslint-plugin-sonarjs` | [docs](https://rules.sonarsource.com/javascript/RSPEC-3525/) |

## Code examples

### Incorrect: the rule reports this

```js
function MyNonClass(initializerArgs = []) {
  this._values = [...initializerArgs];
}

MyNonClass.prototype.doSomething = function () {  // Noncompliant
  // ...
}
```

### Correct: the rule accepts this

```js
class MyClass {
  constructor(initializerArgs = []) {
    this._values = [...initializerArgs];
  }

  doSomething() {
    //...
  }
}
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
export default [
  {
    rules: {
      "sonarjs/class-prototype": "error",
    },
  },
];
```

## Related rules

- [sonarjs/bool-param-default](https://lintradar.dev/rules/sonarjs/bool-param-default.md): Optional boolean parameters should have default value
- [sonarjs/comment-regex](https://lintradar.dev/rules/sonarjs/comment-regex.md): Track comments matching a regular expression
- [sonarjs/aws-iam-all-resources-accessible](https://lintradar.dev/rules/sonarjs/aws-iam-all-resources-accessible.md): IAM policies should not grant access to all account resources
- [sonarjs/composite-assertions](https://lintradar.dev/rules/sonarjs/composite-assertions.md): Composite assertions should be split
- [sonarjs/arrow-function-convention](https://lintradar.dev/rules/sonarjs/arrow-function-convention.md): Braces and parentheses should be used consistently with arrow functions
- [sonarjs/array-constructor](https://lintradar.dev/rules/sonarjs/array-constructor.md): Array constructors should not be used

---

Web page: https://lintradar.dev/rules/sonarjs/class-prototype. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
