Git Product home page Git Product logo

dval's Introduction

Default Value For Missing Properties

Purpose

Undefined and null erros are common in javascript. This function controls the properties, and returns default value if any of the property is null or undefined.

Usage

First argument is default value, it can be object,string,function or what you want.

Second argument is the object.

Other arguments are properties.

Examples

You have an object like this.

var user = {
    name: 'Davut',
    phone: '123456789',
    address: {
        country: 'Turkey'
    //  ,country_code: '+90'
    }
}

Object structure is like above, but when you get the object as a json, and there is no address.country_code in json object.

Problem: You want to get user.adress.country_code, but country_code is undefined, but execution is not failed.

Problem 2: You want to get user.adress.country_code but address is undefined, so you get uncaught type error like this Uncaught TypeError: Cannot read property country_code of undfined then execution is failed.

So I write a basic function, if there is a undefined or null property return the default value

_dval('+90',user,'address','country_code');
// Returns +90 if it is null or undefined.

Actually, you can make same thing like this without a function.

user.address.country_code === undefined || user.adress.country_code === null ? '+90' : user.address.country_code
// Returns +90

It is solution for problem 1, but not problem 2 . Because if adress is undefined, same uncaught typeerror.

So the function is controll from top to sub properties and it is more readable.

dval's People

Contributors

davutkara avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.