Skip to content

unicorn/prefer-prototype-methods Pedantic ​

🛠️ An auto-fix is available for this rule.

What it does ​

This rule prefers borrowing methods from the prototype instead of the instance.

Why is this bad? ​

“Borrowing” a method from an instance of Array or Object is less clear than getting it from the corresponding prototype.

Examples ​

Examples of incorrect code for this rule:

javascript
const array = [].slice.apply(bar);
const type = {}.toString.call(foo);
Reflect.apply([].forEach, arrayLike, [callback]);

Examples of correct code for this rule:

javascript
const array = Array.prototype.slice.apply(bar);
const type = Object.prototype.toString.call(foo);
Reflect.apply(Array.prototype.forEach, arrayLike, [callback]);
const maxValue = Math.max.apply(Math, numbers);

How to use ​

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-prototype-methods
json
{
  "rules": {
    "unicorn/prefer-prototype-methods": "error"
  }
}

References ​

Released under the MIT License.