[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Object/valueOf [Back]  [Original]

Object.prototype.valueOf() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Object.prototype.valueOf()

Baseline

20157

valueOf() Object this

function MyNumberType(n) {
  this.number = n;
}

MyNumberType.prototype.valueOf = function () {
  return this.number;
};

const object = new MyNumberType(4);

console.log(object + 3);
// : 7

js
valueOf()

: valueOf valueOf() primitiveValue.valueOf() Object.prototype.valueOf()

JavaScript valueOf valueOf JavaScript

toString() toString() Object.prototype.toString() valueOf()

Object.prototype null toString() Object.prototype.valueOf() valueOf() valueOf() toString() [Symbol.toPrimitive]() valueOf toString

valueOf()

valueOf() this

js
const obj = { foo: 1 };
console.log(obj.valueOf() === obj); // true

console.log(Object.prototype.valueOf.call("primitive"));
// [String: 'primitive'] (a wrapper object)

valueOf

valueOf

Box valueOf

js
class Box {
  #value;
  constructor(value) {
    this.#value = value;
  }
  valueOf() {
    return this.#value;
  }
}

Box JavaScript

js
const box = new Box(123);
console.log(box + 456); // 579
console.log(box == 123); // true

valueOf JavaScript

js
box.valueOf();

[Symbol.toPrimitive]() valueOf() valueOf() valueOf() toString()

js
+new Date(); //  new Date().getTime() 
+{}; // NaN toString()  "[object Object]" 
+[]; // 0 toString() 
+[1]; // 1 toString()  "1" 
+[1, 2]; // NaN toString()  "1,2" 
+new Set([1]); // NaN toString()  "[object Set]" 
+{ valueOf: () => 42 }; // 42

ECMAScript 2027 LanguageSpecification
# sec-object.prototype.valueof


Web Proxy Viewer  |  New URL  |  Original Page