對象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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章