Valueof (), tostring ()

Source: Internet
Author: User

tag: blog Io OS ar Java data SP Div C

基本上,所有JS数据类型都拥有valueOf和toString这两个方法,null除外。它们俩解决javascript值运算与显示的问题。

JavaScript 的 valueOf() 方法

valueOf() 方法可返回 Boolean 对象的原始值。

用法booleanObject.valueOf(),返回值为booleanObject 的原始布尔值。如果调用该方法的对象不是 Boolean,则抛出异常 TypeError。

var boo = new Boolean(false);console.log(boo.valueOf()); //false 
JavaScript 的 toString() 方法

toString() 方法可把一个逻辑值转换为字符串,并返回结果。

用法 booleanObject.toString(),返回值根据原始布尔值或者 booleanObject 对象的值返回字符串 "true" 或 "false"。如果调用该方法的对象不是 Boolean,则抛出异常 TypeError。

在 Boolean 对象被用于字符串环境中时,此方法会被自动调用。

下面脚本将创建一个 Boolean 对象,并把它转换成字符串:

var boo = new Boolean(true);console.log(boo.toString()); //true

 先看一例:

var aaa = {i: 10,valueOf: function() { return this.i+30; },toString: function() { return this.valueOf()+10; }}console.log(aaa > 20); // trueconsole.log(+aaa); // 40console.log(aaa); // 50

 之所以有这样的结果,因为它们偷偷地调用valueOf或toString方法。但如何区分什么情况下是调用了哪个方法呢,我们可以通过另一个方法测试一下。由于用到console.log,请在装有firebug的FF中实验!

var bbb = {  i: 10,  toString: function() {    console.log(‘toString‘);    return this.i;  },  valueOf: function() {    console.log(‘valueOf‘);    return this.i;  }} console.log(bbb);// 10 toStringconsole.log(+bbb); // 10 valueOfconsole.log(‘‘+bbb); // 10 valueOfconsole.log(String(bbb)); // 10 toStringconsole.log(Number(bbb)); // 10  valueOfconsole.log(bbb == ‘10‘); // true valueOfconsole.log(bbb === ‘10‘); // false

 乍一看结果,大抵给人的感觉是,如果转换为字符串时调用toString方法,如果是转换为数值时则调用valueOf方法,但其中有两个很不和谐。一个 是alert(‘‘+bbb),字符串合拼应该是调用toString方法……另一个我们暂时可以理解为===操作符不进行隐式转换,因此不调用它们。为 了追究真相,我们需要更严谨的实验。

var aa = {  i: 10,  toString: function() {    console.log(‘toString‘);    return this.i;  }} console.log(aa);// 10 toStringconsole.log(+aa); // 10 toStringconsole.log(‘‘+aa); // 10 toStringconsole.log(String(aa)); // 10 toStringconsole.log(Number(aa)); // 10 toStringconsole.log(aa == ‘10‘); // true toString

 再看valueOf。

var bb = {   i: 10,   valueOf: function() {     console.log(‘valueOf‘);     return this.i;   }}  console.log(bb);// [object Object] console.log(+bb); // 10 valueOf console.log(‘‘+bb); // 10 valueOf console.log(String(bb)); // [object Object] console.log(Number(bb)); // 10 valueOf console.log(bb == ‘10‘); // true valueOf

 发现有点不同吧?!它没有像上面toString那样统一规整。对于那个[object Object],我估计是从Object那里继承过来的,我们再去掉它看看。

Object.prototype.toString = null; var cc = {  i: 10,  valueOf: function() {    console.log(‘valueOf‘);    return this.i;  }} console.log(cc);// 10 valueOfconsole.log(+cc); // 10 valueOfconsole.log(‘‘+cc); // 10 valueOfconsole.log(String(cc)); // 10 valueOfconsole.log(Number(cc)); // 10 valueOfconsole.log(cc == ‘10‘); // true valueOf

 如果只重写了toString,对象转换时会无视valueOf的存在来进行转换。但是,如果只重写了valueOf方法,在要转换为字符串的时候会优先 考虑valueOf方法。在不能调用toString的情况下,只能让valueOf上阵了。对于那个奇怪的字符串拼接问题,可能是出于操作符上,翻开 ECMA262-5 发现都有一个getValue操作。嗯,那么谜底应该是揭开了。重写会加大它们调用的优化高,而在有操作符的情况下,valueOf的优先级本来就比 toString的高。

 

valueOf()、toString()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.