聊一聊JavaScript基础巩固提高系列课程(三)---数据类型全解

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据类型ZT</title>
</head>
<body>
1概述
    数值:number
    字符串
    布尔
    null
    undefined
    对象------又可以分为 狭义的对象。数值。函数
2 typeof 运算符
    检测值类型的三种方法:
        typeof方法
        instanceof方法
        Object.prototype.toString()方法
    typeof方法验证:
        console.log(typeof 123)//number
        console.log(typeof '123') //string
        console.log(typeof true)//boolean
        var a = function(){}
        console.log(typeof a)//function
        console.log(typeof undefined)//undefined
        console.log(typeof null)//object
        console.log(typeof window) // "object"
        console.log(typeof {})// "object"
        console.log(typeof []) // "object"
    typeof特性的利用:
        v
        // ReferenceError: v is not defined
        typeof v
        // "undefined"
        这一特性通常用在判断条件中:
        if(v){ }  //        if(typeof v =='undefined'){ }  //    instanceof方法验证:
        var o = [];
        var a = {};
        alert(o instanceof Array) //true
        alert(a instanceof Array) //false
3 null undefined
    两者含义相似,语法效果没有区别
if中两者都会被转化成false
        但是转化为数字时却不一样:
            Number(null) // 0
            5 + null // 5
            ------------------------------
            Number(undefined) // NaN
            5 + undefined // NaN
    用法和含义:
        null表示空值
        undefined表示未定义,下面是返回undefined的典型场景:
        1 变量声明了,但是未给他赋值
            var i ; i//undefined
        2 调用函数时,没有提供返回值,默认返回undefined
            var a= function(){} ;var b = a();  b//undefined
        3 对象没有赋值的属性
            var 0 = {};0.p//undefined
4 布尔值
    if([]){
        console.log(true)
    }
    if({}){
        console.log(true)
    }
    上述结果返回的都是true



</body>
<script>
    console.log(typeof 123)//number
    console.log(typeof '123') //string
    console.log(typeof true)//boolean
    var a = function(){}
    console.log(typeof a)//boolean
    console.log(typeof undefined)//boolean
    console.log(typeof null)//boolean
    var o = [];
    var a = {};
    alert(o instanceof Array)
    alert(a instanceof Array)

</script>
</html>

猜你喜欢

转载自blog.csdn.net/zteenmozart/article/details/80664117