微信小程序之 typeof、instanceof、constructor类型判断

您所在的位置:网站首页 微信小程序变量定义 微信小程序之 typeof、instanceof、constructor类型判断

微信小程序之 typeof、instanceof、constructor类型判断

2024-07-11 07:13| 来源: 网络整理| 查看: 265

写在前面

注意:利用typeof、instanceof、constructor不是所有的数据类型都可以判断出来的,可以搭配使用来判断数据类型。 注意:typeof无法判断出数组,只会把数组当成object打印;【推荐使用】 注意:constructor无法判断出undefined类型。【推荐使用】 注意:instanceof无法判断出字符串、数字、undefined、布尔类型。【不推荐用】

一、利用typeof判断数据类型

我的这篇文章专门写了对typeof类型相关log的打印

利用typeof判断对象类型是否为字符串。以下代码打印typeof---strOne是字符串类型

var strOne = "CoderZb,iOS开发,小程序开发"; if (typeof strOne == 'string') { console.log('typeof---strOne是字符串类型'); } else { console.log('typeof---sstrOne不是字符串类型'); }

利用typeof判断对象类型是否为数字。以下代码打印打印typeof---num是number类型

var num = 7777; if (typeof num == 'number') { console.log('typeof---num是number类型'); } else { console.log('typeof---num不是number类型'); }

利用typeof判断对象类型是否为布尔类型。以下代码打印'typeof---boolValue是布尔类型

var boolValue = true; if (typeof boolValue == 'boolean') { console.log('typeof---boolValue是布尔类型'); } else { console.log('typeof---boolValue不是布尔类型'); }

利用typeof判断对象类型是否为字典object。以下代码打印'typeof---dictOjb是对象类型

var dictOjb = {"1":"2","3":"4"}; if (typeof dictOjb == 'object') { console.log('typeof---dictOjb是对象类型'); } else { console.log('typeof---dictOjb不是对象类型'); }

利用typeof判断对象类型是否为函数。以下代码打印typeof---func是函数类型

var func = function () { }; if (typeof func == 'function') { console.log('typeof---func是函数类型'); } else { console.log('typeof---func不是函数类型'); }

利用typeof判断对象类型是否为undefined。以下代码打印'typeof---date是undefined类型

var date ; if (typeof date == 'undefined') { console.log('typeof---date是undefined类型'); } else { console.log('typeof---date不是undefined类型'); } 二、利用constructor判断数据类型

利用constructor判断对象类型是否为字符串。以下代码打印'constructor---strOne是字符串

var strOne = "CoderZb,iOS开发,小程序开发"; if (strOne.constructor == String) { console.log('constructor---strOne是字符串'); } else { console.log('onstructor---strOne不是字符串'); }

利用constructor判断对象类型是否为数字。以下代码打印'constructor---num是数字类型

var num = 7777; if (num.constructor == Number) { console.log('constructor---num是数字类型'); } else { console.log('constructor---num不是数字类型'); }

利用constructor判断对象类型是否为布尔。以下代码打印'constructor---num是布尔类型

var boolValue = true; if (boolValue.constructor === Boolean) { console.log('constructor---boolValue是布尔类型'); } else { console.log('constructor---boolValue不是布尔类型'); }

利用constructor判断对象类型是否为字典object。以下代码打印'constructor-dictOjb是对象类型

var dictOjb = {"1":"2","3":"4"}; if (dictOjb.constructor == Object) { console.log('constructor-dictOjb是对象类型'); } else { console.log('constructor-dictOjb不是对象类型'); }

利用constructor判断对象类型是否为函数。以下代码打印'constructor-func是函数类型

var func = function () { }; if (func.constructor == Function){ console.log('constructor-func是函数类型'); }else{ console.log('constructor-func不是函数类型'); }

利用constructor判断对象类型是否为数组。以下代码打印constructor---arrayOne是数组类型

var arrayOne = [1, 2, 3, 4]; if (arrayOne.constructor === Array) { console.log('constructor---arrayOne是数组类型'); } else { console.log('constructor---arrayOne不是数组类型'); } 三、利用instanceof判断数据类型

利用instanceof判断对象类型是否为数组。以下代码打印'instanceof---arrayOne是数组类型

var arrayOne = [1, 2, 3, 4]; if (arrayOne instanceof Array) { console.log('instanceof---arrayOne是数组类型'); } else { console.log('instanceof---arrayOne不是数组类型'); }

利用instanceof判断对象类型是否为字典object。以下代码打印'instanceof---dictOjb是对象类型类型

var dictOjb = {"1":"2","3":"4"}; if (dictOjb instanceof Object) { console.log('instanceof---dictOjb是对象类型类型'); } else { console.log('instanceof---dictOjb不是对象类型'); }

利用instanceof判断对象类型是否为函数。以下代码打印'instanceof---Function是函数类型

var func = function () { }; if (func instanceof Function) { console.log('instanceof---Function是函数类型'); } else { console.log('instanceof---Function不是函数类型'); }


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3