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

后端 未结 10 1203
[愿得一人]
[愿得一人] 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:43

    // Typescript
    static nullsafe(instance: T, func: (T) => R): R {
        return func(instance)
    }
    
    // Javascript
    function nullsafe(instance, func) {
        return func(instance);
    };
    
    // use like this
    const instance = getSomething();
    let thing = nullsafe(instance, t => t.thing0.thing1.thingx);
    

提交回复
热议问题