Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

85.JavaScript中如何判断对象是否为空 #85

Open
webVueBlog opened this issue Feb 9, 2023 · 0 comments
Open

85.JavaScript中如何判断对象是否为空 #85

webVueBlog opened this issue Feb 9, 2023 · 0 comments

Comments

@webVueBlog
Copy link
Member

一般使用Object.keys()来判断对象是否为空

const obj = {};

Object.keys(obj).length === 0  // true 则为空对象

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

new Object();

new String();
new Number();
new Boolean();
new Array();
new RegExp();
new Function();
new Date();
new Error();
function badEmptyCheck(value) {
    return Object.keys(value).length === 0;
}

badEmptyCheck(new String()); // true
badEmptyCheck(new Number()); // true
badEmptyCheck(new Boolean()); // true
badEmptyCheck(new Array()); // true
badEmptyCheck(new RegExp()); // true
badEmptyCheck(new Function()); // true
badEmptyCheck(new Date()); // true
badEmptyCheck(new Error();); // true
function goodEmptyCheck(value) {
    return Object.keys(value).length === 0 && value.constructor === Object;
}

goodEmptyCheck(new String()); // false
goodEmptyCheck(new Number()); // false
goodEmptyCheck(new Boolean()); // false
goodEmptyCheck(new Array()); // false
goodEmptyCheck(new RegExp()); // false
goodEmptyCheck(new Function()); // false
goodEmptyCheck(new Date()); // false
goodEmptyCheck(new Error();); // false

Object.keys(null).length === 0;   // TypeError

Object.keys(undefined).length === 0;  // TypeError
function goodEmptyCheck(value) {
    return value && Object.keys(value).length === 0 && value.constructor === Object;
}

getOwnPropertySymbols 和 getOwnPropertyNames

Reflect.ownKeys()

const a = null
const b = undefined
console.log(!a) // true
console.log(!b) // true

通过ES6语法 Object.keys() 进行判断



const a = {
  name: 'a'
}
const b = {}
console.log(Object.keys(a).length === 0) // false
console.log(Object.keys(b).length === 0) // true



const a = {
  name: 'a'
}
const b = {}
console.log(JSON.stringify(a) === '{}') // false
console.log(JSON.stringify(b) === '{}') // true


const a = {
  name: 'a'
}
const b = {}
console.log(Object.getOwnPropertyNames(a).length === 0) // false
console.log(Object.getOwnPropertyNames(b).length === 0) // true


const a = { [Symbol()]: 'a' }

console.log(a) // { [Symbol()]: 'a' }
console.log(JSON.stringify(a) === '{}') // true
console.log(Object.keys(a).length === 0) // true
console.log(Object.getOwnPropertyNames(a).length === 0) // true

const a = { [Symbol()]: 'a' }

console.log(a) // { [Symbol()]: 'a' }
console.log(JSON.stringify(a) === '{}') // true
console.log(Object.keys(a).length === 0) // true
console.log(Object.getOwnPropertyNames(a).length === 0) // true

getOwnPropertySymbols
console.log(Object.getOwnPropertySymbols(a).length === 0) // false

const a = { [Symbol()]: 'a' }
const b = { a: 'a' }
const c = {}

console.log(Object.getOwnPropertyNames(a).length === 0 && Object.getOwnPropertySymbols(a).length === 0) // false
console.log(Object.getOwnPropertyNames(b).length === 0 && Object.getOwnPropertySymbols(b).length === 0)  // false
console.log(Object.getOwnPropertyNames(c).length === 0 && Object.getOwnPropertySymbols(c).length === 0)  // true

const a = { [Symbol()]: 'a' }
const b = { a: 'a' }
const c = {}
console.log(Reflect.ownKeys(a).length === 0) // false
console.log(Reflect.ownKeys(b).length === 0) // false
console.log(Reflect.ownKeys(c).length === 0) // true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant