检查对象数据类型

76 阅读1分钟

检查对象数据类型

      基本数据类型
      typeof

    检查对象数据类型
      instanceof 

    对象 instanceof 数据类型
      arr instanceof Array
       =>true

    对象分类
        内置对象
         Object  Array Data  ...
        自定义对象
        
例
<script>
        //1.封装手机构造函数
        function Phone(color, type) {
            this.color = color  //颜色
            this.type = type  //型号
            //打电话
            this.call = function () {
                console.log(this.type + this.color + '打电话');
            }
            //发短信
            this.sendMessage = function () {
                console.log(this.type + this.color + '发短信');
            }
        }
        function test1() {
            let num = '100'
            console.log(typeof num)
        }
        // test1()
        function test2() {
            let arr = new Array(10, 20, 30)
            // let arr = [10,20.30]
            let p1 = new Phone('白色', 'iphone18')
            let p2 = new Phone('黑色', 'samsang8900')
            // console.log(typeof arr);
            // console.log(typeof p1);
            if( p1 instanceof Phone ){
                alert('p1是Phone手机类型')
            }else{
                alert('p1不是Phone手机类型')
            }
        }
        test2()
    </script>