在ES6 (ES2015/JavaScript.next/Harmony)中是否有一个空安全的属性访问(空传播/存在)操作符?比如CoffeeScript ?还是计划在ES7中?

var aThing = getSomething()
...
aThing = possiblyNull?.thing

大致如下:

if (possiblyNull != null) aThing = possiblyNull.thing

理想情况下,解决方案不应该分配(甚至未定义)一个东西,如果posblynull是空的


当前回答

// Typescript
static nullsafe<T, R>(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);

其他回答

根据这里的列表,目前还没有建议将安全遍历添加到Ecmascript中。所以不仅没有很好的方法,而且在可预见的将来也不会加入。

编辑:由于这篇文章最初是我写的,所以它实际上是被添加到语言中的。

它不像?那样好。运算符,但要实现类似的结果,你可以这样做:

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

因为null和undefined都是假值(参见参考),所以只有当前面的属性不是null或undefined时,才会访问&&运算符后面的属性。

或者,你可以这样写一个函数:

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

用法:

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

或者,使用回退值:

_try(() => user.address.postcode, "none") // return postcode or a custom string

2020年解决方案?和? ?

你现在可以直接使用?(可选链接)内联安全测试的存在。所有现代浏览器都支持它。

?? (Nullish Coalescing)可以用来设置一个默认值,如果是undefined或null。

aThing = possiblyNull ?? aThing
aThing = a?.b?.c ?? possiblyNullFallback ?? aThing

如果属性存在,?。继续进行下一个检查,或返回有效值。任何故障都将立即短路并返回未定义。

const example = {a: ["first", {b:3}, false]}

example?.a  // ["first", {b:3}, false]
example?.b  // undefined

example?.a?.[0]     // "first"
example?.a?.[1]?.a  // undefined
example?.a?.[1]?.b  // 3

domElement?.parentElement?.children?.[3]?.nextElementSibling

null?.()                // undefined
validFunction?.()       // result
(() => {return 1})?.()  // 1

为了确保默认定义的值,可以使用??。如果需要第一个真值,可以使用||。

example?.c ?? "c"  // "c"
example?.c || "c"  // "c"

example?.a?.[2] ?? 2  // false
example?.a?.[2] || 2  // 2

如果不检查大小写,左侧属性必须存在。如果不是,它将抛出异常。

example?.First         // undefined
example?.First.Second  // Uncaught TypeError: Cannot read property 'Second' of undefined

?. 浏览器支持- 92%,2021年11月

?? 浏览器支持- 92%

Mozilla的文档

--

逻辑空赋值,2020+解决方案

新的操作符目前正在添加到浏览器中,??=, ||=和&&=。它们做的并不是你想要的,但根据你代码的目的,它们可能会导致相同的结果。

注意:这些在公共浏览器版本中还不常见,但Babel应该可以很好地编译。将随着可用性更改而更新。

??=检查左边是否未定义或为空,如果已经定义则短路。如果不是,则将左边的值赋给右边的值。||=和&&=类似,但基于||和&&操作符。

基本的例子

let a          // undefined
let b = null
let c = false

a ??= true  // true
b ??= true  // true
c ??= true  // false

对象/数组的例子

let x = ["foo"]
let y = { foo: "fizz" }

x[0] ??= "bar"  // "foo"
x[1] ??= "bar"  // "bar"

y.foo ??= "buzz"  // "fizz"
y.bar ??= "buzz"  // "buzz"

x  // Array [ "foo", "bar" ]
y  // Object { foo: "fizz", bar: "buzz" }

浏览器支持2021年11月- 90%

Mozilla的文档

香草替代安全的财产访问

(((a.b || {}).c || {}).d || {}).e

最简洁的条件赋值可能是这样的

try { b = a.b.c.d.e } catch(e) {}

不,在ES6中没有空传播运算符。你必须选择一个已知的模式。

不过,你可以使用析构:

({thing: aThing} = possiblyNull);

有很多关于在ES7中添加这样一个操作符的讨论(例如这个),但直到几年后可选的链接语法在ES2020中标准化,才真正开始流行起来。