本文共 908 字,大约阅读时间需要 3 分钟。
A问:typeof 是干什么的 B说: 检测数据类型,输出对应的字符串。。。切记是字符串呀!具体如下
"undefined" ------如果某个值为未定义 ; 例如 let a; typeof a , typeof undefined
"boolean"---------如果这个值是布尔值 例如 typeof true, typeof false
"string"-----如果这个值是字符串 例如: typeof 'true'
"number"----如果这个值是数值 例如: typeof 1
"object"------如果这个值是对象或null 例如: let a = null; let b = new object(); typeof a typeof b
"function"------如果这个值是函数
看起来没啥问题呢 ,但是做题不一定做对,具体来看看呢,请输出下列题目的运行结果
var x = [typeof x, typeof y][1]typeof typeof x;解析:1.首先y未定义所以 x = "undefined" 2.执行 typeofof "undefined" 返回结果是 “string” 3.执行 typeof "string"思考: 多个typeof 连用返回的肯定是 "string" 因为typeof 返回的永远是带引号的类型,再判断肯定是string
(function(){ return typeof arguments})();arguments 是数组, 数组也属于对象类型,所以返回object
var f= function g(){ return '酸菜鱼---又酸又菜又多鱼'}typeof g() //报错了 g is not defined,因为g()赋值给变量f了typeof f //返回"function"typeof f() //返回"string"
(function (test){ return typeof test.p;}}({test:{ p : 1}})//输出"undefined"
转载地址:http://kaurf.baihongyu.com/