unicorn/prefer-prototype-methods

Require borrowing a method from the prototype instead of from an empty [] or {}

fixable

[].slice.call(list) creates an array only to reach slice, and then throws it away. Array.prototype.slice.call(list) names where the method comes from and creates nothing.

The rule reports a method of an empty [] or {} that's used with .call(), .apply() or .bind(), or passed as the first argument of Reflect.apply().

--fix replaces [] with Array.prototype and {} with Object.prototype.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/prefer-prototype-methods" = "error" # or "warn"

Examples#

example.js3 problems
const links = [].slice.call(document.links);
const isOwn = {}.hasOwnProperty.call(user, "name");
const first = Reflect.apply([].at, items, [0]);
example.jsNo problems
const links = Array.prototype.slice.call(document.links);
const isOwn = Object.prototype.hasOwnProperty.call(user, "name");
const first = Reflect.apply(Array.prototype.at, items, [0]);

A method of any other object isn't reported:

example.jsNo problems
const log = console.log.bind(console);
const all = [1, 2].concat.call(items, more);