学javascript 必须了解 typeof 和 instanceof

javascript 的基本类型是数字型 字符串 null boolean undefined .复杂类型就是对象 判断基本类型就用typeof 对象就用instanceof  ,啥也不说了看代码 请


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<script>
    function dd(){
    document.writeln(typeof 1); //number
    document.writeln(typeof null);  //object
    document.writeln(typeof (new Number(1))) //number
    document.writeln(typeof (function(){})); //function
    document.writeln(typeof undefined);  //undefined
    document.writeln(typeof []);  //object
    document.writeln(typeof {}); //object
    document.writeln(typeof "abc") //string
    }

    function ss()
    {  //经典的面试题
        document.writeln(typeof 111+"abc");//numberabc
    }

    function ww()
    {
        document.writeln(typeof null===undefined); // false
    }

    function gg()
    {
        var fg=function(){}
        document.writeln(fg instanceof Object); //true

        var df={}
        document.writeln(df instanceof Object); //true

        var arr=[];
        document.writeln(arr instanceof Object); //true
    }

</script>
<body>


<input type="button" value="typeof" οnclick="dd()">
<input type="button" value="typeof 优先级" οnclick="ss()">
<input type="button" value="typeof null===undefined" οnclick="ww()">
<input type="button" value="函数对象数组" οnclick="gg()">
</body>
</html>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值