建设商务网站目的及功能定位,重庆铜梁网站建设费用,阿里云卸载wordpress,制作免费企业宣传视频【函数封装】获取任意数据的数据类型 /*** 获取任意数据的数据类型** param x 变量* returns 返回变量的类型名称#xff08;小写字母#xff09;*/
function getType(x) {// 获取目标数据的私有属性 [[Class]] 的值const originType Object.prototype.toString.call(x); //… 【函数封装】获取任意数据的数据类型 /*** 获取任意数据的数据类型** param x 变量* returns 返回变量的类型名称小写字母*/
function getType(x) {// 获取目标数据的私有属性 [[Class]] 的值const originType Object.prototype.toString.call(x); // 以字符串为例[object String]// 获取类型属性值中 的下标const spaceIndex originType.indexOf( );// 截取类型属性值中 到末尾]之间的字符串const type originType.slice(spaceIndex 1, -1); // 以字符串为例String// 将字符串转换为小写return type.toLowerCase(); //以字符串为例string
}typeof 运算符 适用于检测值类型 null 除外 、函数和类的数据类型对引用类型的数据只能得到 object
参数返回值数值number字符串string布尔型booleanundefinedundefinednullobjectSymbol 数据symbolNaNnumberInfinitynumber函数functionclass类function数组等对象object 检测是否为 null if (x null) {console.log(x 的数据类型为 null);
}检测是否为数组 Array.isArray(val)val instanceof ArrayObject.prototype.toString.call(val) [object Array]val?.constructor ArrayObject.getPrototypeOf(val) Array.prototype// isPrototypeOf() 方法用于测试一个对象是否存在于另一个对象的原型链上。
Array.prototype.isPrototypeOf(val)检测是否为非数组/函数的对象 Object.prototype.toString.call(val) [object Object] val?.constructor ObjectObject.getPrototypeOf(val) Object.prototype