js基础(一):判断类型

6次阅读

共计 3092 个字符,预计需要花费 8 分钟才能阅读完成。

一、常见实例
判断是否为对象
let obj = {}

// 1.Object.prototype.toString

if (Object.prototype.toString.call(obj) === ‘[Object Object]’) {
console.log(‘ 对象!’)
}

// 2.constructor

if (obj.constructor === Object) {
console.log(‘ 对象!’)
}

// 3.$.type()

if ($.type(obj) === ‘object’) {
console.log(‘ 对象!’)
}

// 4.$.isPlainObject() , 用于判断指定参数是否是一个纯粹的对象

if ($.isPlainObject(obj) === ‘object’) {
console.log(‘ 对象!’)
}

判断对象是否为空对象
let obj = {}

// 1.JSON

if (JSON.stringify(obj) === ‘{}’) {
console.log(‘ 空对象!’)
}

// 2.Object.keys(), es6 方法会返回一个由一个给定对象的自身可枚举属性组成的数组

if (Object.keys(obj).length === 0) {
console.log(‘ 空对象!’)
}

// 3. 循环

for (var i in obj) {
return true // 不为空
}
return false // 空对象

// 4. $.isEmptyObject(), 该对象没有属性可以通过 for…in 迭代

if ($.isEmptyObject(obj)) {
console.log(‘ 空对象!’)
}

判断是否为数组
let arr = []

// 1.Object.prototype.toString

if (Object.prototype.toString.call(arr) === ‘[object Array]’) {
console.log(‘ 数组!’)
}

// 2.constructor

if (arr.constructor === Array) {
console.log(‘ 数组!’)
}

// 3.$.type()

if ($.type(arr) === ‘array’) {
console.log(‘ 数组!’)
}

// 4.Array.isArray, 当检测 Array 实例时, Array.isArray 优于 instanceof, 因为 Array.isArray 能检测 iframes

if (arr instanceof Array === true) {
console.log(‘ 数组!’)
}
//Array.isArray 是 ES 5.1 推出的,不支持 IE6~8。假如不存在 Array.isArray(),则在其他代码之前运行下面的代码将创建该方法
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === ‘[object Array]’;
};
}

// 5.instanceof

if (arr instanceof Array === true) {
console.log(‘ 数组!’)
}

判断是否为数组是否为空数组
let arr = []

// 1.length,简易版,已知为数组

if (arr && arr.length > 0) {
console.log(‘ 不为空数组!’)
} else {
console.log(‘ 空数组!’)
}

// 2.Object.prototype.isPrototypeOf,测试一个对象是否存在于另一个对象的原型链上

if (Object.prototype.isPrototypeOf(arr) && Object.keys(arr).length === 0) {
console.log(‘ 空数组!’)
}

// 3.Array.isArray,同样存在兼容问题

if (Array.isArray(arr) && arr.length === 0) {
console.log(‘ 空数组!’)
}

判断字符串为 json
function isJSON(str) {
if (typeof str == ‘string’) {
try {
var obj=JSON.parse(str);
if(typeof obj == ‘object’ && obj){
return true; // 可转
}else{
return false;// 不可转
}

} catch() {
return false; // 不可转
}
}
console.log(‘It is not a string!’)
}

判断字符串是否为数字
let num = ‘3’

// 1.isNaN, null、空格以及空串会被按照 0 来处理

if ((!isNaN(num)) && num != null && num != ”) {
console.log(‘num 为数字!’)
}

// 2. 正则

function isNumber(val) {
var regPos = /^\d+(\.\d+)?$/; // 非负浮点数
var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; // 负浮点数
if(regPos.test(val) || regNeg.test(val)) {
return true;
} else {
return false;
}
}

判断某值为 null 或者为 undefined
// jquery 源码推荐写法,省代码
var obj = {}
if(obj.a == null) {
// 相当于 obj.a === null || obj.a === undefined
}

二、五大常见方法
1.typeof
操作符返回一个字符串,表示未经计算的操作数的类型,用于除 null、对象和数组之外的通用类型的判断方法
2.Object.prototype.toString
原生原型扩展函数,用来精确的区分数据类型,万能
3.$.type()
用于确定 JavaScript 内置对象的类型,并返回小写形式的类型名称,万能
4.instanceof
该运算符用于测试构造函数的 prototype 属性是否出现在对象的原型链中的任何位置,用于检测引用类型的判断方法,针对 Array 和 RegExp 进行判断。
5.constructor
该属性返回对创建此对象的数组函数的引用,每个具有原型的对象都会自动获得 constructor 属性。
注意:类继承时的问题
 function A(){};

function B(){};

 A.prototype = new B(); // A 继承自 B

 var aObj = new A();

 alert(aobj.constructor === B) //true;

 alert(aobj.constructor === A) //false;

//instanceof 方法中对象直接继承和间接继承的都会报 true

alert(aobj instanceof B) //true;

alert(aobj instanceof A) // true;

// 解决 construtor 的问题通常是让对象的 constructor 手动指向自己

aobj.constructor = A; // 将自己的类赋值给对象的 constructor 属性

alert(aobj.constructor === A) // true;

alert(aobj.constructor === B) // false; 基类不会报 true 了;

原博:https://blog.csdn.net/u011684…
参考:typeof,instanceof,$.type][8],[$.isPlainObject(),判断为 json

正文完
 0