QQWQX

快乐工作,健康生活。

0%

JS判断常用数据类型

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"))              //-------->[object String]
console.log(Object.prototype.toString.call(123)) //-------->[object Number]
console.log(Object.prototype.toString.call(true)) //-------->[object Boolean]
console.log(Object.prototype.toString.call([1, 2, 3])) //-------->[object Array]
console.log(Object.prototype.toString.call(null)) //-------->[object Null]
console.log(Object.prototype.toString.call(undefined)) //-------->[object Undefined]
console.log(Object.prototype.toString.call({ name: 'Hello' })) //-------->[object Object]
console.log(Object.prototype.toString.call(function () { })) //-------->[object Function]
console.log(Object.prototype.toString.call(new Date())) //-------->[object Date]
console.log(Object.prototype.toString.call(/\d/)) //-------->[object RegExp]
console.log(Object.prototype.toString.call(Symbol())) //-------->[object 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
//注意当出现继承的时候,使用constructor会出现问题
function A() {};

function B() {};
A.prototype = new B(); //A继承自B
console.log(A.constructor === B) -------->false
var C = new A();
//现在开始判断C是否跟A的构造器一样
console.log(C.constructor === B) -------->true
console.log(C.constructor === A) -------->false
//解决这种情况,通常是手动调整对象的constructor指向
C.constructor = A; //将自己的类赋值给对象的constructor属性
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
通常===出现在我们的条件判断中,比如判断一个变量是否为空