1. 程式人生 > >如何判斷一個物件例項是不是某個型別,如Cat型別

如何判斷一個物件例項是不是某個型別,如Cat型別

<script>
    function cat(){}
    var b = new cat();

    if(b instanceof cat){
        console.log("a是cat")
    }
    if(b.constructor==cat){
        console.log("a是cat")
    }
    //兩種方法都可以
</script>