Article directory
1. How to judge that an object has a certain attribute?
For example: let obj={name:'zhangsan',age:21}
there are the following methods (property is the variable of the property name, which is actually key, the key name):
1. property in obj
The effect is as shown in the figure:
in operator
2. Reflect.has(obj, property)
The effect is as shown in the figure:
About Reflect :
① It is a built-in object of JS without a constructor. It can be used to traverse the key of the object, such as: ; ② It can Reflect.ownKeys(obj) // ['name', 'age']
also
be used to add an attribute to the object, such as: Reflect.set(obj, 'hobby', ['singing','reading'])
, the return value is true/ false, as shown in the figure:
③ Regardless of whether it is a self-owned property or a property on the prototype, Reflect.has(obj,property)
the method will return true.
3. obj.hasOwnProperty(property)
It can be judged whether it is an object's own property , if so, return true, otherwise return false (return false on the prototype chain).
All objects that inherit Object will inherit the hasOwnProperty method. It is used to detect whether an object contains specific own properties; unlike the in operator, this method ignores properties inherited from the prototype chain.
4. Object.hasOwn(obj, property)
It is a method of Object, and it also judges its own properties.
However, pay attention to browser version compatibility issues, Google 93 and above are only supported. Can not be used rashly.
MDN recommends replacing it with this method
hasOwnProperty
. The original words are as follows: "This method is recommended to be used instead of Object.hasOwnProperty(), because it applies to objects created with Object.create(null) and objects that override the inherited hasOwnProperty() method. Although it can be passed in the external object Calling Object.prototype.hasOwnProperty() on Object.prototype.hasOwnProperty() solves these problems, but Object.hasOwn() is more intuitive."
The following is the graph of the running results:
5. Object.prototype.hasOwnProperty.call
Method
Judging own attributes:
Object.prototype.hasOwnProperty.call(obj2,'studentId')
Second, what are the methods of traversing the array?
for,for...in,for...of,forEach,map
Among them, forEach
will change the original array, map will return a new array, and assign the result to a new variable to receive, such as:
const arr=[1, 2];
const result = arr.map((item, index)=>{
return item + 1;
})
// result:[2,3] arr还是[1,2]
Notice:
for...in
What you get is the array subscript, andfor...of
what you get is the value of the array;for...in
If you can traverse the object, you get the objectkey
;for...of
if you cannot traverse the object, you will report an error obj is not iterable.
If traversal with additional functions:
1、reduce
2、filter
3、some
4、indexOf
5、includes
The blogger here is just a simple list.
What are these methods used for? You can check it in the rookie tutorial or mdn, and you can see the summary of this student .
3. The latest front-end experience and market analysis in 2023
Click here: front-end