一、typeof取值类型范围
1、string
console.log(typeof("abc"))
2、number
console.log(typeof(123))
3、boolean
console.log(typeof(true))
4、undefined
console.log(typeof(x))
5、object
console.log(typeof({a:10,b:20}))
6、function
console.log(typeof(function(){}))
7、symbol (ES6新增)
console.log(typeof Symbol())
二、typeof和instanceof的区别
1、typeof
typeof运算符可以返回一个数据类型
<script>
console.log(typeof 123) //number
console.log(typeof '123') //string
console.log(typeof false) //boolean
console.log(typeof(x)) //undefined
console.log(typeof([1,'a',true])) //object
console.log(typeof({a:10,b:20})) //object
console.log(typeof(null)) //object
console.log(typeof(new Number(10))) //object
console.log(typeof(function(){})) //function
console.log(typeof Symbol()) //Symbol
</script>
返回类型
在上述中array和object显示都是object那他们要怎么去区分呢?
2、instanceof
instanceof可以识别继承的类型
<script>
var items = [];
var object = {};
function reflect(value) {
return value;
}
console.log(items instanceof Array); //true
console.log(items instanceof Object); //true
console.log(items instanceof Function); // false
console.log(object instanceof Object); //true
console.log(object instanceof Array); //false
console.log(object instanceof Function) ; //false
console.log(reflect instanceof Function); // true
console.log(reflect instanceof Array); //false
console.log(reflect instanceof Object); //true
</script>
上述生成结果