string.valueOf

string.valueOf

valueOf()方法返回一个String对象的原始值(primitive value)。

语法

str.valueOf()

返回值

表示给定String对象的原始值的字符串。

描述

String 对象的 valueOf 方法返回一个String对象的原始值。该值等同于String.prototype.toString()

该方法通常在 JavaScript 内部被调用,而不是在代码里显示调用。

示例

使用valueOf()

var x = new String('Hello world' console.log(x.valueOf() // Displays 'Hello world'

规范

SpecificationStatusComment
ECMAScript 1st Edition (ECMA-262)StandardInitial definition. Implemented in JavaScript 1.1.
ECMAScript 5.1 (ECMA-262)The definition of 'String.prototype.valueOf' in that specification.Standard
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'String.prototype.valueOf' in that specification.Standard
ECMAScript Latest Draft (ECMA-262)The definition of 'String.prototype.valueOf' in that specification.Draft

浏览器兼容性

FeatureChromeFirefoxEdgeInternet ExplorerOperaSafari
Basic Support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)

FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS Safari
Basic Support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)