valueOf 和 toSring

来源:互联网 发布:电脑锣编程 编辑:程序博客网 时间:2024/06/05 08:21

toString()函数用于将当前对象以字符串的形式返回

该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法。

所有主流浏览器均支持该函数

valueOf()函数用于返回指定对象的原始值

该方法属于Object对象,由于所有的对象都"继承"了Object的对象实例,因此几乎所有的实例对象都可以使用该方法。


语法

object.valueOf( )

object.toString( )

返回值

toString()函数的返回值为String类型。返回当前对象的字符串形式。

JavaScript的许多内置对象都重写了该函数,以实现更适合自身的功能需要。

类型行为描述Array将 Array 的每个元素转换为字符串,并将它们依次连接起来,两个元素之间用英文逗号作为分隔符进行拼接。Boolean如果布尔值是true,则返回"true"。否则返回"false"。Date返回日期的文本表示。Error返回一个包含相关错误信息的字符串。Function返回如下格式的字符串,其中 functionname 是一个函数的名称,此函数的 toString 方法被调用: "function functionname() { [native code] }"Number返回数值的字符串表示。还可返回以指定进制表示的字符串,请参考Number.toString()。String返回 String 对象的值。Object(默认)返回"[object ObjectName]",其中 ObjectName 是对象类型的名称。

valueOf()函数返回指定对象的原始值。

JavaScript的许多内置对象都重写了该函数,以实现更适合自身的功能需要。因此,不同类型对象的valueOf()方法的返回值和返回值类型均可能不同。

对象返回值Array数组实例对象。Boolean布尔值。Date以毫秒数存储的时间值,从 UTC 1970 年 1 月 1 日午夜开始计算。Function函数本身。Number数字值。Object对象本身。这是默认设置。String字符串值。

示例&说明

以下示例运行的宿主环境为Windows 7 简体中文旗舰版 64位,地点为中国大陆。不同的区域设置和语言设置,执行的输出结果可能不同。

//数组var array = ["CodePlayer", true, 12, -5];document.writeln( array.toString() ); // CodePlayer,true,12,-5// 日期var date = new Date(2013, 7, 18, 23, 11, 59, 230);document.writeln( date.toString() ); // Sun Aug 18 2013 23:11:59 GMT+0800 (中国标准时间)// 日期2var date2 = new Date(1099, 7, 18, 23, 11, 59, 230);document.writeln( date2.toString() ); // Fri Aug 18 1099 23:11:59 GMT+0800 (中国标准时间)// 数字var num =  15.26540;document.writeln( num.toString() ); // 15.2654// 布尔var bool = true;document.writeln( bool.toString() ); // true// Objectvar 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]


示例&说明

// 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或falsevar 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 ); // truevar 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
0 0
原创粉丝点击