データタイプを判断するJS方式とデータタイプのチェックを実現するためのカプセル化方式。

** 1。typeof **

 console.log( typeof(1) );//number
 
 console.log( typeof "abc" );//string

console.log(typeof false); //boolean

console.log(typeof function () {
    
    }); //function

console.log(typeof {
    
    }); //object

console.log(typeof undefined); //undefined
		//typeof无法判断出数组、对象、null的具体类型,

2.インスタンスの

 console.log( [1,2,3]  instanceof  Array);//true
 
 console.log( {
    
    "name":"a"}  instanceof Array );//false
 
 console.log( "abcde" instanceof String );//false

  

****

3.Object.prototype.toString.call

****

console.log(    Object.prototype.toString.call( "234" )  );//string
	
console.log(    Object.prototype.toString.call( null )  );//null
	
console.log(    Object.prototype.toString.call( [1] )  );//Array

4.パッケージ関数はデータタイプを検出します。

 function checkType( date){
    
    
            var type=Object.prototype.toString.call(date);
            type= type.substr( 8,type.length-9 );
            return type;
        };

        var x=checkType(1);
        console.log(x);//number

おすすめ

転載: blog.csdn.net/w1666793979/article/details/111396771