轉載請註明預見才能遇見的博客:http://my.csdn.net/
原文地址:https://blog.csdn.net/pcaxb/article/details/101020581
JavaScript原生系列-數據類型、數據類型判斷、數據類型轉換
目錄
JavaScript原生系列-數據類型、數據類型判斷、數據類型轉換
4.Object.prototype.toString.call()判斷一個變量是對象還是數組
1.數據類型
基本數據類型:Number,String,Boolean,Null,Undefined
複雜數據類型:Object
es6新增:Symbol
// 打印封裝
function L() {
console.log.apply(this,arguments);
}
2.判斷數據類型
判斷簡單數據類型可以用typeof;判斷一個變量是對象還是數組使用 instanceof,constructor和Object.prototype.toString.call(),最好使用Object.prototype.toString.call(),更加精準
1.typeof 判斷基本數據類型
typeof返回的是字符串
string:字符串類型
number:數字類型,包括整形,浮點型,NaN
function:function
object:數組,對象,null
boolean:true,false
undefined:undefined
symbol:symbol
let a1= "12";
let b1 = 12;
let c1 = false;
let d1 = null;
let e1 = undefined;
let f1 = [];
let g1 = {};
let h1 = Symbol();
console.log(typeof(a1));//string --
console.log(typeof(b1));//number --
console.log(typeof(c1));//boolean--
console.log(typeof(d1));//object --null
console.log(typeof(e1));//undefined--
console.log(typeof(f1));//object --[]
console.log(typeof(g1));//object --{}
console.log(typeof(h1));//symbol --
//函數、NAN
console.log(typeof(function(){}));//function
console.log(typeof(NaN));//number
缺點:typeof不能區分數組,對象,null,返回的都是object
2.instanceof判斷一個變量是對象還是數組
使用instanceof Object要注意null,null是對象,但是返回false。
//判斷一個變量是不是對象
var arr = [1,2]
var obj = {name:"cc"}
var a = 111;
var nl = null;
L(arr instanceof Object);//true
L(obj instanceof Object);//true
L(a instanceof Object);//false
L(nl instanceof Object);//false 判斷錯誤
//判斷一個變量是不是數組
L(arr instanceof Array)//true
L(obj instanceof Array)//false
L(a instanceof Array);//false
L(nl instanceof Array);//false
instanceof原理:JS原型鏈與instanceof底層原理
3.constructor判斷一個變量是對象還是數組
不能判斷null,空沒有constructor,不能簡單的constructor用判斷是不是對象。
//判斷一個變量是不是對象
var arr = [1,2]
var obj = {name:"cc"}
var a = 111;
var nl = null;
L(arr.constructor === Object);//false 判斷錯誤,需要一層一層找才能判斷
L(obj.constructor === Object);//true
L(a.constructor === Object);//false
// L(nl.constructor === Object);//語法錯誤
//判斷一個變量是不是數組
L(arr.constructor === Array)//true
L(obj.constructor === Array)//false
L(a.constructor === Array);//false
// L(nl.constructor === Array);//語法錯誤
4.Object.prototype.toString.call()判斷一個變量是對象還是數組
Object.prototype.toString.call()方法可以精準判斷變量類型,它返回[object constructorName]的字符串格式,這裏的constructorName就是call參數的函數名
var arr = [1,2]
var obj = {name:"cc"}
var a = 111;
var nl = null;
L(Object.prototype.toString.call(arr));//[object Array]
L(Object.prototype.toString.call(obj));//[object Object]
L(Object.prototype.toString.call(a));//[object Number]
L(Object.prototype.toString.call(nl));//[object Null]
//判斷一個變量是不是數組
L(Object.prototype.toString.call(arr) === "[object Array]");//true
L(Object.prototype.toString.call(obj) === "[object Array]");//false
L(Object.prototype.toString.call(a) === "[object Array]");//false
L(Object.prototype.toString.call(nl) === "[object Array]");//false
3.數據類型轉換-顯式轉換
利用js提供的函數parseInt() , parseFloat() , Number() , Boolean() , String()進行數據轉換
1.parseInt()
1.1) 忽略字符串前面的空格,直至找到第一個非空字符,還會將數字後面的非數字的字符串去掉。
1.2) 如果第一個字符不是數字符號或者符號,返回NaN
1.3) 會將小數取整。(向下取整)
L(parseInt("2312")); //2312
L(parseInt("12.365")); //12
L(parseInt("yuci")); //NaN
L(parseInt("10")); //返回 10
L(parseInt("19",10)); //返回 19 (10+9)
L(parseInt("11",2)); //返回 3 (2+1)
L(parseInt("17",8)); //返回 15 (8+7)
L(parseInt("1f",16)); //返回 31 (16+15)
L(parseInt("010")); //未定:返回 10
L(parseInt("+123")); //123
L(parseInt([1,2,4])); //1
L(parseInt("")); //NaN
2.parseFloat()
parseFloat()與parseInt()一樣,唯一區別是parseFloat()可以保留小數
3.Number()
3.1) 如果轉換的內容本身就是一個數值類型的字符串,那麼將來在轉換的時候會返回自己。
3.2) 如果要轉換的內容是空的字符串,那以轉換的結果是0.
3.3) 如果是其它的字符,那麼將來在轉換的時候結果是NaN.
L(Number("123")); //123
L(Number("hello world")); //NaN
L(Number("")); //0
L(Number([])); //0
L(Number([1])); //1
L(Number([1,2])); //NaN
L(Number(null)); //0
L(Number(new Object())); //NaN
4.Boolean()
除了"",0,false,null,undefined,NaN返回false,其他全部返回true(包括空對象,空數組)
L(Boolean(123));//true
L(Boolean(false));//false
L(Boolean(""));//false
L(Boolean(0));//false
L(Boolean(null));//false
L(Boolean(undefined));//false
L(Boolean(NaN));//false
L(Boolean({}));//true
L(Boolean([]));//true
5.String()
toString()的方法來進行轉換(包裝類)toString()的參數是轉換的進制格式(幾進制)。undefined,null不能用toString()
var number = 10;
L(number.toString()); //"10"
L(number.toString(2)); //"1010"
L(number.toString(8)); //"12"
L(number.toString(10)); //"10"
L(number.toString(16)); //"a"
L(String(null)); //"null"
var str = null;
// L(str.toString()); //Cannot read property 'toString' of null
var number1;
L(String(number1)); //undefined
// L(undefined.toString()); //Cannot read property 'toString' of undefined
L(String(undefined))//undefined
4.數據類型轉換-隱式轉換
+ - * / %進行隱式轉換,還有比較運算符(> < ==);js中的隱式轉換是字符串和數字之間的轉換 ;+ 會把數字轉換成字符串;* / % > < == 把字符串轉化成數字
var num = "123";
var num2 = 126;
L(+num); //123
L(num+num2); //"123126"
L(num2-num); //126-123=3
L(num2*num); //15498
L(num2/num); //1.024390243902439
L(num2%num); //3
L(num2>num); //true
var number = 1;
L(!!number); //true
JavaScript原生系列-數據類型、數據類型判斷、數據類型轉換