点击打开视频讲解
<template>
<div id="app">
<h2>JavaScript判断是否为空对象的几种方法h2>
<button @click="handerNullObj">1、空对象对应的字符串为 "{}"button>
<button @click="handerForIn">2、for inbutton>
<button @click="handerGetOwnPropertyNames">3、Object.getOwnPropertyNames()button>
<button @click="handerKeys">4、ES6 的 Object.keys()button>
div>
template>
<script>
export default {
name: 'App',
data(){
return {
}
},
methods:{
handerNullObj(){
let data = {};
let b = JSON.stringify(data) == "{}";
console.log(b);
},
handerForIn(){
let obj = {};
let b = function () {
for (let key in obj) {
return false;
}
return true;
};
console.log(b());
},
handerGetOwnPropertyNames(){
let data = {};
let arr = Object.getOwnPropertyNames(data);
console.log(arr.length == 0);
},
handerKeys(){
let data = {};
let arr = Object.keys(data);
console.log(arr.length == 0);
},
}
}
script>
<style scoped>
style>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58