|
|
@@ -0,0 +1,80 @@
|
|
|
+<!DOCTYPE html>
|
|
|
+<html lang="en">
|
|
|
+<head>
|
|
|
+ <meta charset="UTF-8">
|
|
|
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
+ <title>Document</title>
|
|
|
+</head>
|
|
|
+<body>
|
|
|
+ <script>
|
|
|
+ let str = "hello";
|
|
|
+ let num = 10;
|
|
|
+ let bool = true;
|
|
|
+ let und = undefined;
|
|
|
+ let nul = null;
|
|
|
+ let arr = [1,2,3,4,5,6,7,8,9];
|
|
|
+ let obj = {a:10};
|
|
|
+ let fun = function () {
|
|
|
+ console.log("hello");
|
|
|
+ }
|
|
|
+
|
|
|
+ // typeof 运算符
|
|
|
+ // typeof 运算符可以判断一个变量的数据类型
|
|
|
+ // 返回一个字符串 表示数据类型
|
|
|
+ // 一般用作于判断基本数据类型,除了null 和对象返回都是object
|
|
|
+ // console.log(typeof str); // string
|
|
|
+ // console.log(typeof num); // number
|
|
|
+ // console.log(typeof bool); // boolean
|
|
|
+ // console.log(typeof und); // undefined
|
|
|
+ // console.log(typeof nul); // object
|
|
|
+ // console.log(typeof arr); // object
|
|
|
+ // console.log(typeof obj); // object
|
|
|
+
|
|
|
+ // instanceof 运算符
|
|
|
+ // instanceof 运算符可以判断一个对象是否是一个类的实例
|
|
|
+ // 返回一个布尔值
|
|
|
+ // 一般用作于判断引用数据类型 数组 对象 函数
|
|
|
+ // console.log(str instanceof String); // false
|
|
|
+ // console.log(num instanceof Number); // false
|
|
|
+ // console.log(bool instanceof Boolean); // false
|
|
|
+ // console.log(und instanceof undefined); // false
|
|
|
+ // console.log(nul instanceof null); // false
|
|
|
+ // console.log(arr instanceof Array); // true
|
|
|
+ // console.log(obj instanceof Object); // true
|
|
|
+ // console.log(fun instanceof Function); // true
|
|
|
+
|
|
|
+ // constructor 构造函数
|
|
|
+ // constructor 可以判断一个变量的数据类型
|
|
|
+ // 返回一个字符串 表示数据类型
|
|
|
+ // 除了 null 和 undefined 其他都可以使用 constructor 来判断数据类型
|
|
|
+ // console.log(str.constructor); // function String() { [native code] }
|
|
|
+ // console.log(num.constructor); // function Number() { [native code] }
|
|
|
+ // console.log(bool.constructor); // function Boolean() { [native code] }
|
|
|
+ // console.log(und.constructor); // undefined
|
|
|
+ // console.log(nul.constructor); // null
|
|
|
+ // console.log(arr.constructor); // function Array() { [native code] }
|
|
|
+ // console.log(obj.constructor); // function Object() { [native code] }
|
|
|
+ // console.log(fun.constructor); // function Function() { [native code] }
|
|
|
+
|
|
|
+ // 通过 Object.prototype.toString.call() 来判断数据类型
|
|
|
+ // 可以判断所有数据类型
|
|
|
+ // 返回一个带有类型名称的字符串
|
|
|
+ // console.log(Object.prototype.toString.call(str)); // [object String]
|
|
|
+ // console.log(Object.prototype.toString.call(num)); // [object Number]
|
|
|
+ // console.log(Object.prototype.toString.call(bool)); // [object Boolean]
|
|
|
+ // console.log(Object.prototype.toString.call(und)); // [object Undefined]
|
|
|
+ // console.log(Object.prototype.toString.call(nul)); // [object Null]
|
|
|
+ // console.log(Object.prototype.toString.call(arr)); // [object Array]
|
|
|
+ // console.log(Object.prototype.toString.call(obj)); // [object Object]
|
|
|
+ // console.log(Object.prototype.toString.call(fun)); // [object Function]
|
|
|
+
|
|
|
+
|
|
|
+ // 判断数组
|
|
|
+ console.log(Array.isArray(arr))
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ </script>
|
|
|
+</body>
|
|
|
+</html>
|