No Preview

Rizwan Khan

Posted on 5 January at 22:59

Stop breaking your code by accessing undefined value

I have written a utility funtion to get value from a object and if value is not present then it returns default value given by you.

eg:

what if any property of myObj not available then it will break your code and user cant see anything.

My Solution to objects

use getValue function to get either right value or user given default value

if c value available then it will return c value else it will return null

getValue(object, path_to_property, user_default_value)



Above Solution will work with all older and newer browsers

Another Simplest solution to the problem is modern es6 syntax

lets use same example

if i try to access something which is not present within the object

if i try to complicate things a little more

p is undefined and now i am trying to access a property of undefined got error boom

Modern Solution to the problem which will work with all modern browsers

0

23

2

No Preview

Rizwan Khan

India

Coding is an art and I love art

Explore more similar Post

0 Comments

Loading..