vue3 判断数据类型
时间: 2023-10-19 11:08:28 浏览: 38
在Vue3中,可以使用typeof和instanceof关键字来判断数据类型。
1. 使用typeof关键字判断数据类型:
typeof操作符可以返回一个字符串,表示操作数的数据类型。
示例:
```
let num = 123;
console.log(typeof num); // 输出:number
let str = 'hello';
console.log(typeof str); // 输出:string
let bool = true;
console.log(typeof bool); // 输出:boolean
let arr = [1, 2, 3];
console.log(typeof arr); // 输出:object
let obj = { name: 'Tom', age: 18 };
console.log(typeof obj); // 输出:object
let fun = function() {};
console.log(typeof fun); // 输出:function
```
2. 使用instanceof关键字判断数据类型:
instanceof操作符可以判断某个对象是否为某个构造函数的实例。
示例:
```
let num = 123;
console.log(num instanceof Number); // 输出:false
let str = 'hello';
console.log(str instanceof String); // 输出:false
let bool = true;
console.log(bool instanceof Boolean); // 输出:false
let arr = [1, 2, 3];
console.log(arr instanceof Array); // 输出:true
let obj = { name: 'Tom', age: 18 };
console.log(obj instanceof Object); // 输出:true
let fun = function() {};
console.log(fun instanceof Function); // 输出:true
```