JavaScript运算符typeof和instanceof的区别

JavaScript运算符typeof和instanceof的区别,第1张

        // typeof 作用: 检测数据类型,返回: 小写字母字符串, *** 作数: 简单数据类型/函数/对象
        console.log(typeof(undefined));     // undefined
        console.log(typeof(true));          // boolean
        console.log(typeof(false));         // boolean
        console.log(typeof(5));             // number
        console.log(typeof(4.5555));        // number
        console.log(typeof('4'));           // string
        console.log(typeof("4"));           // string
        console.log(typeof(function() {})); // function
        console.log(typeof(Symbol));        // function
        console.log(typeof(Symbol()));      // Symbol
        console.log(typeof(new Function()));//function

        console.log(typeof([]));              //object
        console.log(typeof({}));              //object
        console.log(typeof(null));            //object
        console.log(typeof(new Array()));     //object
        console.log(typeof(new Object()));    //object
        console.log(typeof(new Error()));     //object
        console.log(typeof(new Number()));    //object
        console.log(typeof(new String()));    //object
        console.log(typeof(new Date()));      //object
        console.log(typeof(new Boolean()));   //object


        // instanceof 作用: 检测对象之间的关联性, 返回: 布尔值, 
            *** 作数: 左边的类型值一定是引用类型,右边必须是函数
        console.log(new Number(999) instanceof Number);     // true
        console.log(new String('999') instanceof String);   // true
        console.log(new Array() instanceof Array);          // true
        console.log(new Object() instanceof Object);        // true

        console.log(9999 instanceof Number);                // false
        console.log('string' instanceof String);            // false
        console.log(true instanceof Boolean);               // false
        console.log([] instanceof Array);                   // true
        console.log({} instanceof Object);                  // true

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/796047.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-06
下一篇 2022-05-06

发表评论

登录后才能评论

评论列表(0条)

保存