How to implement object Verification by js
Editor to share with you how to achieve js object verification, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Object verification mode
If we have an object like this:
Const parent = {child: {child1: {child2: {key: 10}
Most of the time, we will write like this to prevent the non-existence of a certain level from causing an error:
Parent & & parent.child & & parent.child.child1 & & parent.child.child1.child2
This makes the code look bloated, and you can use JavaScript's optional chain operator:
Parent?.child?.child1?.child2
This implementation and effect is the same as the long list above.
The optional chain operator also applies to arrays:
Const array = [1,2,3]; array?. [5]
The optional chain operator allows us to read the values of properties deep in the chain of connected objects without explicitly verifying that each reference in the chain is valid. It does not cause an error if the reference is empty (null or undefined), and the expression short circuit returns a value of undefined. When used with a function call, returns undefined if the given function does not exist.
These are all the contents of the article "how to implement object validation in js". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!