1.typeof(一般判断基本数据类型用这个方法)
1 2 3 4 5 6 7 8 9 10 11
| console.log(typeof "helloworld") -------->"string" console.log(typeof 123) -------->"number" console.log(typeof [1, 2, 3]) -------->"object" console.log(typeof new Function()) -------->"function" console.log(typeof new Date()) -------->"object" console.log(typeof new RegExp()) -------->"object" console.log(typeof Symbol()) -------->"symbol" console.log(typeof true) -------->"boolean" console.log(typeof null) -------->"object" console.log(typeof undefined) -------->"undefined" console.log(typeof 'undefined') -------->"string"
|
注意:打印的结果都是以字符串呈现
2.instance of()
1 2 3
| console.log([1, 2] instanceof Array) ---->"true" console.log(new Function() instanceof Function); ---->"true" console.log(new Date() instanceof Date); ---->"true"
|
3. Object.prototype.toString.call(万金油,哪里都可以用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| console.log(Object.prototype.toString.call("123")) console.log(Object.prototype.toString.call(123)) console.log(Object.prototype.toString.call(true)) console.log(Object.prototype.toString.call([1, 2, 3])) console.log(Object.prototype.toString.call(null)) console.log(Object.prototype.toString.call(undefined)) console.log(Object.prototype.toString.call({ name: 'Hello' })) console.log(Object.prototype.toString.call(function () { })) console.log(Object.prototype.toString.call(new Date())) console.log(Object.prototype.toString.call(/\d/)) console.log(Object.prototype.toString.call(Symbol()))
Object.prototype.hasOwnProperty.call({name: '1'}, 'name')
|
注意:注意区分大小写. toString方法,在Object原型上返回数据格式
4.constructor()
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function A() {};
function B() {}; A.prototype = new B(); console.log(A.constructor === B) -------->false var C = new A();
console.log(C.constructor === B) -------->true console.log(C.constructor === A) -------->false
C.constructor = A; console.log(C.constructor === A); -------->true console.log(C.constructor === B); -------->false
|
注意:null和undefined没有constructor;判断数字时使用(),比如 (123).constructor,如果写成123.constructor会报错,constructor在类继承时会出错,因为Object被覆盖掉了,检测结果就不对了
5.jquery.type(万能法)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| console.log(jQuery.type(undefined) === "undefined") -------->true console.log(jQuery.type() === "undefined"); -------->true console.log(jQuery.type(window.notDefined) === "undefined") -------->true console.log(jQuery.type(123) === "number") -------->true console.log(jQuery.type('123') === "string") -------->true console.log(jQuery.type([]) === "array") -------->true console.log(jQuery.type(true) === "boolean") -------->true console.log(jQuery.type(function(){}) === "function") -------->true console.log(jQuery.type(new Date()) === "date") -------->true console.log(jQuery.type(/\d/) === "regexp") -------->true console.log(jQuery.type(new Error()) === "error") -------->true console.log(jQuery.type({name:'Hello'}) === "object") ------->true console.log(jQuery.type(Symbol()) === "symbol") ------->true ------->其余对象类型一般返回object
|
注意:在使用时,一定要引入jquery文件,不然会报错,jQuery is not defined
6.严格运算符===
1
| 通常===出现在我们的条件判断中,比如判断一个变量是否为空
|