Skip to content

Latest commit

 

History

History
54 lines (42 loc) · 1.25 KB

no-valueof-field.md

File metadata and controls

54 lines (42 loc) · 1.25 KB

Forbid the creation of valueOf fields

The valueOf field in objects is used to implicitly convert an object to a primitive value. Having this field overridden can have implicit unwanted results, or introduce side-effects in code that looks pure, as demonstrated in this article.

Here's an example of a use of valueOf:

const object1 = {
  value: 15,
  valueOf: function() { return this.value; }
};
const object2 = {
  value: 25,
  valueOf: function() { return this.value; }
};
object1 + object2
// => 40

And its more explicit version without the use of valueOf:

const addValueOf = (a, b) => a.value + b.value;

const object1 = {
  value: 15
};
const object2 = {
  value: 25
};

addValueOf(object1, object2)
// => 40

This rule reports any explicit assignment or creation of a valueOf field in an object.

Fail

const object = { valueOf: Math.random };
const object = { valueOf };
const object = { ["valueOf"]: Math.random };
object.valueOf = Math.random;
object.prototype.valueOf = Math.random;

Pass

const object = { value: 2 };
object.value = 2;