类型检测

类型检测小结:

typeof:适合基本类型及function检测,遇到null失效。

[[Class]]:通过{}.toString拿到,适合内置对象和基元类型,遇到null和undefined失效(IE678等返回[object Object]).

instanceof:适合自定义对象,也可以用来检测原生对象,在不同iframe和window间检测时失效。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>Document</title>

    

<script type="text/javascript">
// var a = [10,1,2,3,4,8,9];
// function max(a){
//                         var len = a.length-1;
//                         alert(a.length-1)
//                         var maxnum=a[len];
//                         while (len--) {
//                  maxnum=Math.max(maxnum,a[len])
//                         }
//                         return maxnum;
//                 }
//                 alert(max(a));
var num =32;
num="this is a string"
console.log(32+32);//64
console.log("32"+32);//3232
console.log("32"-32);//0
var x='the answer is'+42;
var y=42+'is the answer';
console.log(x);//the answer is42
console.log(y);//42is the answer
console.log("37"-7);//30
console.log("37"+7)//377
console.log(null-0);//0
console.log(null);//null
//巧用+/—规则转换类型:num-0变量null转为数字;数字变成字符串num+"".
//等和严格等:
console.log("1.23"==1.23);//true
console.log(0==false);//true
console.log(null==undefined);//true
console.log(new Object()==new Object());//false
console.log([1,2]==[1,2])//false
//console.log(a===b);
//严格等,等号两边的类型:
console.log(null===null);//true
console.log(undefined===undefined);//true
console.log(NaN===NaN);//false
console.log(new Object()===new Object());//false
//不严格等:类型相同,同===;类型不同,尝试类型转换和比较
console.log(null==undefined);//ture
//console.log(number==string);
console.log(1=="1.0");//true
//console.log(boolean==number);
console.log(1==true);//true
//console.log(object==number|string);
console.log(new String('hi')=='hi')//true
// var str="string"//undefined
// var strObj=new String("string")//undefined
// str//"string"//0"s",1 "t",2 "r",3"i",4"n",5"g".
// str//"string"
// str.length//6
// str.t=10//10
// str.t//undefind
// str.t=3//3
// var number=123//undefined
// var numObj=new Number("number")//undefined
// number.length//undefined
// number+number//246
// number+""//"123"
//类型检测
//基本类型 函数对象typeof
console.log(typeof 100)//number
console.log(typeof true)//boolean
console.log(typeof function(){})//function
console.log(typeof(undefined))//undefined
console.log(typeof new Object())//object
console.log(typeof [1,2])//array
console.log(typeof NaN)//number*
console.log(typeof null)//object*
//obj instanceof Object
 console.log([1,2] instanceof Array)//true
 console.log(new Object() instanceof Array)//false
 console.log()
console.log()
//Object.prototype.toString
console.log(Object.prototype.toString.apply([1,2,3]))//[object Array]
console.log(Object.prototype.toString.apply(function(){
    var a=1,b=2,c;
    return c=a+b;
}))//[object Function]
console.log(Object.prototype.toString.apply(null))//[object Null]
console.log(Object.prototype.toString.apply(undefined))//[object Undefined]

</script>
</head>
<body>

</body>

</html>
原文地址:https://www.cnblogs.com/xl900912/p/4218293.html