Null-safe property access (and conditional assignment) in ES6/2015

后端 未结 10 1077
[愿得一人]
[愿得一人] 2020-11-22 13:00

Is there a null-safe property access (null propagation / existence) operator in ES6 (ES2015/JavaScript.next/Harmony) like ?. in

相关标签:
10条回答
  • 2020-11-22 13:45

    No, there is no null propagation operator in ES6. You will have to go with one of the known patterns.

    You may be able to use destructuring, though:

    ({thing: aThing} = possiblyNull);
    

    There are many discussions (e.g. this) to add such an operator in ES7, but none really took off.

    0 讨论(0)
  • 2020-11-22 13:45

    I thought this question needed a bit of a refresh for 2018. This can be done nicely without any libraries using Object.defineProperty() and can be used as follows:

    myVariable.safeGet('propA.propB.propC');
    

    I consider this safe (and js-ethical) because of the writeable and enumerable definitions now available for the defineProperty method of Object, as documented in MDN

    function definition below:

    Object.defineProperty(Object.prototype, 'safeGet', { 
        enumerable: false,
        writable: false,
        value: function(p) {
            return p.split('.').reduce((acc, k) => {
                if (acc && k in acc) return acc[k];
                return undefined;
            }, this);
        }
    });
    

    I've put together a jsBin with console output to demonstrate this. Note that in the jsBin version I've also added a custom exception for empty values. This is optional, and so I've left it out of the minimal definition above.

    Improvements are welcomed

    0 讨论(0)
  • 2020-11-22 13:55

    It's not as nice as the ?. operator, but to achieve a similar result you could do:

    user && user.address && user.address.postcode
    

    Since null and undefined are both falsy values (see this reference), the property after the && operator is only accessed if the precedent it not null or undefined.

    Alternatively, you could write a function like this:

    function _try(func, fallbackValue) {
        try {
            var value = func();
            return (value === null || value === undefined) ? fallbackValue : value;
        } catch (e) {
            return fallbackValue;
        }
    }
    

    Usage:

    _try(() => user.address.postcode) // return postcode or undefined 
    

    Or, with a fallback value:

    _try(() => user.address.postcode, "none") // return postcode or a custom string
    
    0 讨论(0)
  • 2020-11-22 13:59

    Update (2020-01-31): Seems people are still finding this, here's the current story:

    • Optional Chaining specification (Stage 4): https://github.com/tc39/proposal-optional-chaining
    • babel-preset-env: This is probably what you want https://babeljs.io/docs/en/babel-preset-env
    • Babel v7 Plugin: https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining

    Update (2017-08-01): If you want to use an official plugin, you can try the alpha build of Babel 7 with the new transform. Your mileage may vary

    https://www.npmjs.com/package/babel-plugin-transform-optional-chaining

    Original:

    A feature that accomplishes that is currently in stage 1: Optional Chaining.

    https://github.com/tc39/proposal-optional-chaining

    If you want to use it today, there is a Babel plugin that accomplishes that.

    https://github.com/davidyaha/ecmascript-optionals-proposal

    0 讨论(0)
提交回复
热议问题