valueOf()
method returns the primitive value of the specified object.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
object.valueOf()
The primitive value of the specified object.
A
(unary) plus sign
can sometimes be used as a shorthand for
valueOf
, e.g. in
+new Number()
). Also see
Using unary plus
.
JavaScript calls the
valueOf
method to convert an object to a primitive value. You rarely need to invoke the
valueOf
method yourself; JavaScript automatically invokes it when encountering an object where a primitive value is expected.
默认情况下,
valueOf
method is inherited by every object descended from
Object
. Every built-in core object overrides this method to return an appropriate value. If an object has no primitive value,
valueOf
returns the object itself.
可以使用
valueOf
within your own code to convert a built-in object into a primitive value. When you create a custom object, you can override
Object.prototype.valueOf()
to call a custom method instead of the default
Object
方法。
You can create a function to be called in place of the default
valueOf
method. Your function must take no arguments.
Suppose you have an object type
MyNumberType
and you want to create a
valueOf
method for it. The following code assigns a user-defined function to the object's
valueOf
method:
MyNumberType.prototype.valueOf = function() { return customPrimitiveValue; };
With the preceding code in place, any time an object of type
MyNumberType
is used in a context where it is to be represented as a primitive value, JavaScript automatically calls the function defined in the preceding code.
An object's
valueOf
method is usually invoked by JavaScript, but you can invoke it yourself as follows:
myNumberType.valueOf()
注意:
Objects in string contexts convert via the
toString()
method, which is different from
String
objects converting to string primitives using
valueOf
. All objects have a string conversion, if only "
[object
type
]
". But many objects do not convert to number, boolean, or function.
function MyNumberType(n) {
this.number = n;
}
MyNumberType.prototype.valueOf = function() {
return this.number;
};
var myObj = new MyNumberType(4);
myObj + 3; // 7
+"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
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'Object.prototype.valueOf' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
valueOf
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 3 | Safari 1 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 10.1 | Safari iOS 1 | Samsung Internet Android 1.0 | nodejs Yes |
完整支持
Object
Object.assign()
Object.create()
Object.defineProperties()
Object.defineProperty()
Object.entries()
Object.freeze()
Object.fromEntries()
Object.getOwnPropertyDescriptor()
Object.getOwnPropertyDescriptors()
Object.getOwnPropertyNames()
Object.getOwnPropertySymbols()
Object.getPrototypeOf()
Object.is()
Object.isExtensible()
Object.isFrozen()
Object.isSealed()
Object.keys()
Object.preventExtensions()
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.seal()
Object.setPrototypeOf()
Object.values()
Function