js的類型轉換詳解
js的類型轉換是js的基礎,必須要掌握的,我做了一下例子分析,大家可以直接復制運行:
"100";
{};
Number(str);
parseInt(str,8);//parseInt只能用于字符串轉為數字
bool
!!(str);
Object(str);
typeof(str_num1) + " str_num1:" +
str_num1);
" str_num2:" +
str_num2);
" str_num3:" +
str_num3);
bool:<br>type:"+ typeof(str_bool1) +
" str_bool1:" +
str_bool1);
" str_bool2:" +
str_bool2);
object:<br>type:"+ typeof(str_obj1) +
" str_obj1:" +
str_obj1);
= String(num);
num + "1";
to bool
!!(num);
Object(num);
to string:<br>type:"+ typeof(num_str1) +
" num_str1:" +
num_str1);
" num_str2:" +
num_str2);
" num_str3:" +
num_str3);
" num_str4:" +
num_str4);
bool:<br>type:"+ typeof(num_bool1) +
" num_bool1:" +
num_bool1);
" num_bool2:" +
num_bool2);
object:<br>type:"+ typeof(num_obj1) +
" num_obj1:" + num_obj1);
transform to num
bool - 2;
string
"2";
object
Object(bool);
to number:<br>type:"+ typeof(bool_num1) +
" bool_num1:" +
bool_num1);
" bool_num2:" +
bool_num2);
" bool_num3:" +
bool_num3);
string:<br>type:"+ typeof(bool_str1) +
" bool_str1:" +
bool_str1);
" bool_str2:" +
bool_str2);
" bool_str3:" +
bool_str3);
object:<br>type:"+ typeof(bool_obj1) +
" bool_obj1:" +
bool_obj1);
Number(obj);
parseInt(obj);
String(obj);
obj.toString();
Boolean(obj);
!!(obj);
to number:<br>type:"+ typeof(obj_num1) +
" obj_num1:" +
obj_num1);
" obj_num2:" +
obj_num2);
" obj_num3:" +
obj_num3);
string:<br>type:"+ typeof(obj_str1) +
" obj_str1:" +
obj_str1);
" obj_str2:" +
obj_str2);
" obj_str3:" +
obj_str3);
bool:<br>type:"+ typeof(obj_bool1) +
" obj_bool1:" +
obj_bool1);
" obj_bool2:" + obj_bool2);
+ "<br>"); //false
//false
"<br>");//true
"<br>");//true
"<br>");//false
"<br>");//false
"<br>");//true
+ "<br>"); //0
//0
"<br>");//NaN
"<br>");//200
"<br>");//0
"<br>");//NaN
"<br>");//NaN
//0
//1
+ "<br>"); //返回空格
//[object Object]
"<br>");//h3
"<br>");//200
"<br>");//0
"<br>");//[object Object]
"<br>");//[object Object]
//false
//true
結果如下:
string to
num:
type:number
type:number
type:number
string
to
bool:
type:boolean
type:boolean
string
to object:
type:object
num to
string:
type:string
type:string
type:string
type:string
num
to
bool:
type:boolean
type:boolean
num
to object:
type:object
bool to
number:
type:number
type:number
type:number
bool
to
string:
type:string
type:string
type:string
bool
to object:
type:object
object
to
number:
type:number
type:number
type:number
object
to string:
type:string
Object]
type:string
Object]2
type:string
object
to
bool:
type:boolean
type:boolean
Boolean()的用法false
false
true
true
false
false
true
Number()的用法0
0
NaN
200
0
NaN
NaN
false
true
Object()的用法
[object
Object]
hi
200
0
[object Object]
[object
Object]
false
true
下面是實驗總結:
類型轉換分為三種方式:(1)強類型轉換(2)轉換函數轉換(3)弱類型轉換
- 強類型轉換:Number(value); String(value); Boolean(value); Object(value)
- 轉換函數轉換:parseInt(value); parseFloat(value)
- 弱類型轉換:var str = str - 0; var num = num + "2"; var num = !!num;
- 轉換為number類型方法: Number(value); parseInt(value);
parseFloat(value),注意:只有對String類型調用這些方法,這兩個函數才能正確運行;對其他類型返回的是NaN(Not a Number)
var value = value - 1; 若value為非數字形式的字符串或對象,返回NaN - 轉換為string類型方法: String(value); var value = value + "A"
若value為對象類型,則對象首先執行toString()方法,轉化為字符串再與"A"相連; value.toString(); - 轉換為boolean類型方法: Boolean(value); !!(value);
- 轉換為object類型方法: Object(value)
另外,Number(),Boolean(),Object()的用法可以參考實驗結果。