javascript - Best way to add null checker to querySelector in jquery -


i have parsed xml , i'm using queryselector

for example:

this.queryselector("type").textcontent 

sometimes value of this.queryselector("type") null previous line returns error.

what beautiful way avoid errors?

i'm doing following:

example = this.queryselector("type") != null ? this.queryselector("type").textcontent : "" //ugly! 

but long , looks bad.

other way: own function findinxml(this, "type"), when want deeper it's looks that:

findinxml(findinxml(this, "type"), "sth") // ugly! 

i prefer use way example:

this.findxml("type").findxml("sth") //looks nice 

one call queryselector:

var typeel; var example = (typeel = this.queryselector("type")) ? typeel.textcontent : ''; 

jsperf agrees:

https://jsperf.com/nullqueryselector/1


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -