您现在的位置:首页 >> 前端 >> 内容

学javascript必须了解typeof和instanceof

时间:2016/11/24 9:25:00 点击:

  核心提示:javascript 的基本类型是数字型 字符串 null boolean undefined .复杂类型就是对象 判断基本类型就用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" onclick="dd()">
<input type="button" value="typeof 优先级" onclick="ss()">
<input type="button" value="typeof null===undefined" onclick="ww()">
<input type="button" value="函数对象数组" onclick="gg()">
</body>
</html>

 

Tags:学1 10 06 6A 
作者:网络 来源:well386的博客