对象valueOf()方法、toString()方法、toLocaleString()方法小结

1.valueOf()用于返回指定对象的原始值

// Array:返回数组对象本身
var array = ["CodePlayer", true, 12, -5];
document.writeln( array.valueOf() === array ); // true

// Date:当前时间距1970年1月1日午夜的毫秒数
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
document.writeln( date.valueOf() ); // 1376838719230

// Number:返回数字值
var num = 15.26540;
document.writeln( num.valueOf() ); // 15.2654

// 布尔:返回布尔值true或false
var bool = true;
document.writeln( bool.valueOf() === bool ); // true
// new一个Boolean对象
var newBool = new Boolean(true);
// valueOf()返回的是true,两者的值相等
document.writeln( newBool.valueOf() == newBool ); // true
// 但是不全等,两者类型不相等,前者是boolean类型,后者是object类型
document.writeln( newBool.valueOf() === newBool ); // false

// Function:返回函数本身
function foo(){
}
document.writeln( foo.valueOf() === foo ); // true
var foo2 = new Function("x", "y", "return x + y;");
document.writeln( foo2.valueOf() === foo2 ); // true

// Object:返回对象本身
var obj = {name: "张三", age: 18};
document.writeln( obj.valueOf() === obj ); // true

// String:返回字符串值
var str = "http://www.365mini.com";
document.writeln( str.valueOf() === str ); // true
// new一个字符串对象

var str2 = new String("http://www.365mini.com");
// 两者的值相等,但不全等,因为类型不同,前者为string类型,后者为object类型
document.writeln( str2.valueOf() === str2 ); // false



2.toString()将当前对象以字符串的形式返回,返回值为String类型

//数组
var array = ["Code", true, 1, -2];
document.writeln( array.toString() ); // Code,true,1,-2

// 日期
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
document.writeln( date.toString() ); // Sun Aug 18 2013 23:11:59 GMT+0800 (中国标准时间)

// 数字
var num = 15.26540;
document.writeln( num.toString() ); // 15.2654

// 布尔
var bool = true;
document.writeln( bool.toString() ); // true

// Object  返回"[object ObjectName]",其中 ObjectName 是对象类型的名称
var obj = {name: "张三", age: 18};
document.writeln( obj.toString() ); // [object Object]

// HTML DOM 节点
var eles = document.getElementsByTagName("body");
document.writeln( eles.toString() ); // [object NodeList]
document.writeln( eles[0].toString() ); // [object HTMLBodyElement]

//function 返回如下格式的字符串,其中 functionname 是一个函数的名称,此函数的 toString 方法被调用: "function functionname() { [native code] }"

3. toLocaleString()函数用于将当前对象以字符串值的形式返回,该字符串的格式适合当前宿主环境的当前区域设置。

// 数组
var array = ["CodePlayer", true, 12, -5];
document.writeln( array.toLocaleString() ); // CodePlayer,true,12,-5

// 日期
var date = new Date(2013, 7, 18, 23, 11, 59, 230);
document.writeln( date.toLocaleString() ); // 2013年8月18日 下午11:11:59

// 数字
var num = 15.26540;
document.writeln( num.toLocaleString() ); // 15.265

// 布尔
var bool = true;
document.writeln( bool.toLocaleString() ); // true

// Object
var obj = {name: "张三", age: 18};
document.writeln( obj.toLocaleString() ); // [object Object]

// HTML DOM 节点
var eles = document.getElementsByTagName("body");
document.writeln( eles.toLocaleString() ); // [object NodeList]
document.writeln( eles[0].toLocaleString() ); // [object HTMLBodyElement


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值