10
0

18_判断数据类型.html 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. // Function Array Object ...
  11. var str = "hello";
  12. var num = 10;
  13. var boo = true;
  14. var arr = [1,2];
  15. var obj = {a:1};
  16. var foo = function(){console.log(1)};
  17. var und = undefined;
  18. var nu = null;
  19. // 方法一
  20. // console.log( typeof str );
  21. // console.log( typeof num);
  22. // console.log( typeof boo);
  23. // console.log( typeof arr);
  24. // console.log( typeof obj);
  25. // console.log( typeof foo);
  26. // console.log( typeof und);
  27. // console.log( typeof nu);
  28. // 方法二
  29. // instanceof 只能判断 object array function
  30. // console.log( arr instanceof Array );
  31. // console.log( obj instanceof Object );
  32. // console.log( str instanceof String );
  33. // console.log( foo instanceof Function );
  34. // 方法三
  35. // console.log(arr.constructor == Array)
  36. // console.log(str.constructor == String);
  37. // console.log(num.constructor == Number);
  38. // console.log(foo.constructor == Function);
  39. // console.log(obj.constructor == Object);
  40. // console.log(boo.constructor == Boolean);
  41. // 方法四
  42. // console.log( Object.prototype.toString.call(und) );
  43. // console.log( Object.prototype.toString.call(nu) );
  44. // console.log( Object.prototype.toString.call(str) );
  45. // console.log( Object.prototype.toString.call(num) );
  46. // console.log( Object.prototype.toString.call(foo) );
  47. // console.log( Object.prototype.toString.call(boo) );
  48. // console.log( Object.prototype.toString.call(arr) );
  49. // console.log( Object.prototype.toString.call(obj) );
  50. </script>
  51. </body>
  52. </html>