A guide to avoiding pits : When taking properties of an object ( Especially the logarithm group length When attribute ), The first step before writing code is to ensure that the object is not null perhaps undefined
, Confirm to “ really ” In this case, take the corresponding attribute value again .

Front end development time , We often pass . Operator to get the property value , There's a hole here that's easy to make bug, Especially the logarithm group length When attribute , It may be caused by carelessness JS Execution error
( Keep it secret , Specifically speaking ) Cause page crash .

for instance , For example, the back-end interface returns data Field type is array , Some common ways for us to deal with the array traversal :
for(let i = 0; i < data.length; i++) { // doSomething... }
The above code has no problem when the back end returns the array normally .

however , Back end return data by null Time ,JS Error will be reported when executing relevant code :Uncaught TypeError: Cannot read property
'length' of null, Page direct white screen . This kind of mistake is easy to miss in the test , Leading to online bug produce .

therefore , At work , When we take the property of an object , We must pay attention : The first step is to ensure that the object is not null perhaps undefined, Take the corresponding attribute after confirming the object is true .
as follows :
// perhaps if(data){ ... }, ensure data by “ Truth value ”, Next step if(Array.isArray(data)) { const len
= data.length //doSomething... }
This type of bug It also gives us a warning : Our front end must have defensive programming
Thinking of , The fields defined by the backend cannot be 100% To trust it , But we should do a good job in dealing with errors and exceptions , So you can avoid a lot of potential bug, Improve the robustness of our front-end code .

Technology
©2019-2020 Toolsou All rights reserved,
C Language programming to find a student's grade java Realize the function of grabbing red packets Random forest R Language implementation QCustomPlot series (5)- Real time dynamic curve Software testing BUG describe use C++ I want to talk to you “ Prototype mode ” ( copy / copy constructor )JS How to operate China's longest high speed rail officially opened ! The fastest way to finish the race 30.5 hour Zimi apologizes :33W Gan charger delayed due to force majeure 2021 year 2 Chinese programming language ranking