判断数据类型的方式

判断数据类型的方式

typeof

console.log(typeof undefined); // undefined
console.log(typeof null); // object
console.log(typeof "123"); // string
console.log(typeof new String("abc")); // object
console.log(typeof 123); // number
console.log(typeof []); // object
console.log(typeof new Array(1)); // object
console.log(typeof {}); // object
console.log(typeof true); // boolean
console.log(typeof function () {}) // function

null和array通过typeof判断为object,如果使用new关键字进行声明,同样是object

var a = "1123";
console.log(typeof a); // string
console.log(typeof b); // undefined

调用一个未声明的变量会报错,但是使用typeof判断未声明的变量会返回undefined。
建议避免在声明时赋值undefined,避免出现上述情况。

instanceof

console.log(true instanceof Boolean); // false
console.log(true instanceof String); // false
console.log(123 instanceof Number); // false
console.log("123" instanceof String); // false
console.log(new String("123") instanceof String); // true
console.log(null instanceof Object); // false
console.log(null instanceof Object); // false
console.log([] instanceof Array); // true
console.log([] instanceof Array); // true
console.log([] instanceof Object); // true
console.log(new String("123") instanceof String); // true
console.log(new String("123") instanceof Object); // true

这里用instanceof判断数组、字符串和Object的关系,仍是true,推断instanceof应该是通过原型链来判断的。

使用constructor判断

console.log("123".constructor); // String
console.log(new String("123").constructor); // String
console.log((123).constructor); // Number
console.log({}.constructor); // Object
console.log([].constructor); // Array
console.log(null.constructor); // 报错
console.log(undefined.constructor); // 报错

null和undefined没有constructor

Object.prototype.toString.call()

Object.prototype.toString.call()是一个最为精准的方法

console.log(Object.prototype.toString.call(123)); // Number
console.log(Object.prototype.toString.call("123")); // String
console.log(Object.prototype.toString.call(new String(123))); // String
console.log(Object.prototype.toString.call({})); // Object
console.log(Object.prototype.toString.call([])); // Array
console.log(Object.prototype.toString.call(undefined)); // Undefined
console.log(Object.prototype.toString.call(null)); // Null
console.log(Object.prototype.toString.call(new Date())); // Date
console.log(Object.prototype.toString.call(/[a-zA-Z0-9]/g)); // RegExp
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue中,判断数据类型可以使用typeof操作符或instanceof操作符。使用typeof操作符可以判断基本数据类型,例如number、boolean、string、function、object和undefined。但是它不能准确判断null和array的类型。例如,typeof null的结果是'object',typeof array的结果也是'object'。对于判断对象类型,可以使用instanceof操作符。例如,判断一个变量是否是Vue组件,可以使用变量名 instanceof Vue。这种方式可以准确判断对象的类型。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [Vue前端面试题总结(二) 数据类型判断详解](https://blog.csdn.net/Rick_and_mode/article/details/108600279)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [前端 vue js判断数据类型常用的方法 [typeof,instanceof,prototype,constructor,jquery.type(), ===]](https://blog.csdn.net/weixin_44972441/article/details/122347004)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值