- let n = 10,
- str = "hello",
- b = true,
- nu = null,
- un;
- let arr = [1,2,3,4];
- let obj = {};
-
- typeof(n) // number
- typeof(str) // string
- typeof(b) // blooean
- typeof(nu) // object
- typeof(un) // undefined
- typeof(arr) // object
- typeof(obj) // object
-
判断方法
- //判断是不是一个数组
- let arr = [1,2,3,4];
- obj.__proto__ == Array.prototype; //true
Object.getPrototypeOf(arr) === Array.prototype;
Array.prototype.isPrototype(arr);
arr.constructor == Array; //true
arr instanceof Array; //true
以上五种有一个缺陷就是将对象的原型对象改成数组的原型对象,以上五种都失效,
- let obj = {};
- obj.__proto__ = Array.prototype;
- obj instanceof Array; // true.
- obj.constructor == Array; //true
- //对象
- let obj = { };
- obj.__proto__ = Array.prototype;
- obj.toString = [object,Object];
-
- //数组的原型对象是重写了toString 方法默认会取出数组里面的内容转成字符串并用逗号间隔!!
-
- let arr = [1,2,3,4];
- arr.toString();
- //输出结果是: 1,2,3,4;
-
- //所以要用.call()方法,调用顶端的原型对象。
-
- Object.prototype.toString.call(arr);
- //输出 [object Array]
-
- //判断方法
- Object.prototype.toString.call(arr)=[object Array]
-
- let arr = [1,2,3,4];
- Array.isArray(arr)