“This is the 17th day of my participation in the First Challenge 2022.

Knowledge to prepare

Before checking the JS void method, we will first understand the following three methods.

Object.keys

The object.keys () method takes an Object as an argument and returns an array of matched properties and methods within that Object

  var obj = {
    name: "cxy",
    age: "19"
  }; 
  var objArray = Object.getOwnPropertyNames(obj);
  console.log(objArray)
Copy the code

You can see that objArray is the return value, and the return value is an array of the contents of the properties within the object

Object.getOwnPropertyNames

Object. GetOwnPropertyNames () method is also same as objects as parameters, return a contains the matched within the Object properties and methods of the array

What’s the difference? Object. GetOwnPropertyNames () can return all of the attributes, and the Object. The keys () only returns an enumerable properties, aye? So what is an enumerable property? Don’t worry, let me explain what an enumerable property is, okay

Enumerable property

An enumerable property is an object that uses the enumerable flag. By default, an object that uses an attribute like obj. Name = “cxy” is true, but it is not enumerable when it is false. When we apply for, object.keys (), json.stringify (), the non-enumerable property is not found, we can understand that the non-enumerable property is invisible

Now we get the above Object. GetOwnPropertyNames and Object. The keys () of actual combat, for example, we add the age attribute for the Object through defineProperty, because this method can be set enumeration logo, here set to false, You can see the following two different returns

var stuObj = {
    name: "cxy"
}
Object.defineProperty(stuObj, 'age', {
  value: "18",
  enumerable: false
});
console.log(Object.keys(stuObj))
console.log(Object.getOwnPropertyNames(stuObj))
Copy the code

hasOwnProperty

HasOwnProperty () is used to determine whether an object contains an attribute. The parameter is the attribute name

  var stuObj = {
    name: "cxy"
  }
  console.log(stuObj.hasOwnProperty('name'))
Copy the code

But one thing to note here is that hasOwnProperty() returns false for inherited properties, which are properties that the object inherits from the prototype, such as toString

Counting empty method

JSON.stringifySentenced to empty

This approach is simpler, using json.stringify to convert the object to a string and then using equal to determine whether the object is empty or not

let obj = {
    name: "cxy"
}
console.log(JSON.stringify(obj) == '{}')
Copy the code

for inSentenced to empty

For in returns false when the loop is triggered and true when the object is empty

let forNull = (items) => {
    for (let item in items) {
        return false
    }
    return true
}
Copy the code

Object.getOwnPropertyNamesSentenced to empty

Here using the above-mentioned Object. GetOwnPropertyNames, returns the length of the array as the judgment basis.

let stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)
Copy the code

Object.keys()Sentenced to empty

As in the previous method, use an array as a criterion

let stuArray = Object.getOwnPropertyNames(obj)
console.log(stuArray.length === 0)
Copy the code

hasOwnPropertySentenced to empty

HasOwnProperty uses the for loop to check the element if it contains it and return false to indicate that it is not empty, otherwise it is empty

let forNull = (items) => {
    for (let item in items) {
        if(items.hasOwnProperty(item)){
            return false
        }
    }
    return true
}
Copy the code