我将举例解释:

猫王运算符(?:)

“猫王运算符”是缩写 Java的三元运算符。一个 这很方便的例子是 返回一个“合理的默认值” 如果表达式解析为false或 null。一个简单的例子是这样的 这样的:

def gender = user.male ? "male" : "female"  //traditional ternary operator usage

def displayName = user.name ?: "Anonymous"  //more compact Elvis operator

安全导航操作员(?.) 使用安全导航操作符 来避免NullPointerException。 通常当你有一个参考 您可能需要验证的对象 在访问前它不是空的 对象的方法或属性。 为了避免这种情况,安全航行 运算符将简单地返回null 而不是抛出异常,比如 所以:

def user = User.find( "admin" )           //this might be null if 'admin' does not exist
def streetName = user?.address?.street    //streetName will be null if user or user.address is null - no NPE thrown

当前回答

我创建了一个软件包,使它更容易使用。

NPM jsdig Github jsdig

你可以处理简单的东西,比如and object:

const world = {
  locations: {
    europe: 'Munich',
    usa: 'Indianapolis'
  }
};

world.dig('locations', 'usa');
// => 'Indianapolis'

world.dig('locations', 'asia', 'japan');
// => 'null'

或者更复杂一点:

const germany = () => 'germany';
const world = [0, 1, { location: { europe: germany } }, 3];
world.dig(2, 'location', 'europe') === germany;
world.dig(2, 'location', 'europe')() === 'germany';

其他回答

在很晚的时候,有一个建议[1]的可选链接目前在第二阶段,与巴别塔插件[2]可用。目前在我所知道的任何浏览器中都没有。

https://github.com/tc39/proposal-optional-chaining https://www.npmjs.com/package/@babel/plugin-proposal-optional-chaining

我认为下面是相当于安全导航的操作符,虽然有点长:

var streetName = user && user.address && user.address.street;

streetName将是user.address.street或undefined的值。

如果你想让它默认为其他东西,你可以结合上面的快捷方式或给出:

var streetName = (user && user.address && user.address.street) || "Unknown Street";

?? 将在js中工作,这相当于?:在kotlin

你可以自己卷:

function resolve(objectToGetValueFrom, stringOfDotSeparatedParameters) {
    var returnObject = objectToGetValueFrom,
        parameters = stringOfDotSeparatedParameters.split('.'),
        i,
        parameter;

    for (i = 0; i < parameters.length; i++) {
        parameter = parameters[i];

        returnObject = returnObject[parameter];

        if (returnObject === undefined) {
            break;
        }
    }
    return returnObject;
};

像这样使用它:

var result = resolve(obj, 'a.b.c.d'); 

*如果a, b, c或d中的任何一个未定义,则结果未定义。

2020年更新

JavaScript现在有了Elvis操作符和安全导航操作符的等价物。


安全出入物业

可选的链接运算符(?.)目前是ECMAScript第四阶段的提案。你今天可以用它来对付巴别塔。

// `undefined` if either `a` or `b` are `null`/`undefined`. `a.b.c` otherwise.
const myVariable = a?.b?.c;

逻辑与运算符(&&)是处理这种情况的“旧的”、更冗长的方法。

const myVariable = a && a.b && a.b.c;

提供默认值

空合并运算符(??)目前是第4阶段的ECMAScript提案。你今天可以用它来对付巴别塔。如果运算符的左边是一个空值(null/undefined),它允许您设置一个默认值。

const myVariable = a?.b?.c ?? 'Some other value';

// Evaluates to 'Some other value'
const myVariable2 = null ?? 'Some other value';

// Evaluates to ''
const myVariable3 = '' ?? 'Some other value';

逻辑或运算符(||)是一种行为略有不同的替代解决方案。它允许您在操作符左侧为假值时设置默认值。注意下面的myVariable3的结果与上面的myVariable3不同。

const myVariable = a?.b?.c || 'Some other value';

// Evaluates to 'Some other value'
const myVariable2 = null || 'Some other value';

// Evaluates to 'Some other value'
const myVariable3 = '' || 'Some other value';