一、查值的类型(可用于拷贝)
1 /* 2 * @Author: [email protected] 3 * @Date: 2017-12-20 15:07:06 4 * @purpose 获取一个值的类型 5 * @param {variateName} target: 要获取类型的变量名或对象 6 * @output {string} result || "null": 返回的参数 - result或者null,为字符串形式的 7 */ 8 function getType(target) { 9 if(target === null){ 10 console.log(target) 11 return "null"; 12 } 13 let result = typeof (target); 14 if (result == "object") { 15 if (target instanceof Array) { 16 result = "array"; 17 } else if (target instanceof Object) { 18 result = "object"; 19 } 20 } 21 console.log(result) 22 return result;//返回类型值的字符串形式 23 }
应用:
1 var nu= null;// null 怎么算 2 var un= undefined; 3 var c = new Array; 4 var d = {}; 5 console.log(getType(c),getType(d),getType(nu),getType(un))
原文地址:https://www.cnblogs.com/padding1015/p/9879219.html
时间: 2024-10-08 04:59:13