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

Object.prototype.valueOf() - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Object.prototype.valueOf()

Baseline Widely available

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

valueOf() .

In this article

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

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

const object1 = new MyNumberType(4);

console.log(object1 + 3);
// Expected output: 7

js
object.valueOf();

.

: () valueOf . , . +new Number(). . .

JavaScript valueOf . .

Object valueOf . valueOf . , valueOf .

valueOf . valueOf Object .

valueOf

valueOf . .

MyNumberType , valueOf . valueOf .

js
MyNumberType.prototype.valueOf = function () {
  return customPrimitiveValue;
};

MyNumberType JavaScript .

valueOf JavaScript .

js
myNumberType.valueOf();

: - toString() , String valueOf . , "[object type]" . , , .

valueOf

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

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

var myObj = new MyNumberType(4);
myObj + 3; // 7

js
+"5"; // 5 (string to number)
+""; // 0 (string to number)
+"1 + 2"; // NaN (doesn't evaluate)
+new Date(); // same as (new Date()).getTime()
+"foo"; // NaN (string to number)
+{}; // NaN
+[]; // 0 (toString() returns an empty string list)
+[1]; // 1
+[1, 2]; // NaN
+new Set([1]); // NaN
+BigInt(1); // Uncaught TypeError: Cannot convert a BigInt value to a number
+undefined; // NaN
+null; // 0
+true; // 1
+false; // 0

Specification
ECMAScript 2027 LanguageSpecification
# sec-object.prototype.valueof


Web Proxy Viewer  |  New URL  |  Original Page