在JavaScript中是否存在null-coalescing(Elvis)运算符或安全导航运算符? [英] Is there a null-coalescing (Elvis) operator or safe navigation operator in javascript?

查看:166
本文介绍了在JavaScript中是否存在null-coalescing(Elvis)运算符或安全导航运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会用例子来解释:

Elvis Operator(?:)


猫王操作员是Java三元运算符的缩短
。如果表达式解析为false或
null,
返回一个'合理的默认'值
,这是非常方便的一个
实例。一个简单的例子可能如下所示:




  def gender = user.male ? male:female//传统的三元运算符用法

def displayName = user.name?:Anonymous//更简洁的Elvis操作符




安全导航操作员(?。)



使用
来避免NullPointerException。
通常,当您有一个对象的引用时,您可能需要在访问对象的
方法或属性之前验证
是否为空。
为避免这种情况,安全导航
操作符将简单地返回空
,而不是抛出一个异常,如
如此:



  def user = User.find(admin)//如果'admin'不存在,则可能为空
def streetName = user?.address?.street // streetName将会为null,如果user或user.address为null - 没有NPE抛出




例如 displayname = user.name || 匿名

但是Javascript目前没有其他功能。如果您需要其他语法,建议您查看 CoffeeScript 。它有一些简写,类似于你正在寻找的。

例如存在的操作符

  zip = lottery.drawWinner?()。address?.zipcode 

功能快捷键

 () - > //相当于function(){} 

性感函数调用

  func'arg1','arg2'//相当于func('arg1','arg2')
pre>

还有多行注释和类。很明显,你必须将它编译为javascript或者以< script type ='text / coffeescript>'的形式插入页面,但它增加了很多功能:)。使用< script type ='text / coffeescript'> 实际上仅用于开发而非生产。


I'll explain by example:

Elvis Operator (?: )

The "Elvis operator" is a shortening of Java's ternary operator. One instance of where this is handy is for returning a 'sensible default' value if an expression resolves to false or null. A simple example might look like this:

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

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

Safe Navigation Operator (?.)

The Safe Navigation operator is used to avoid a NullPointerException. Typically when you have a reference to an object you might need to verify that it is not null before accessing methods or properties of the object. To avoid this, the safe navigation operator will simply return null instead of throwing an exception, like so:

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

解决方案

You can use the logical 'OR' operator in place of the Elvis operator:

For example displayname = user.name || "Anonymous" .

But Javascript currently doesn't have the other functionality. I'd recommend looking at CoffeeScript if you want an alternative syntax. It has some shorthand that is similar to what you are looking for.

For example The Existential Operator

zip = lottery.drawWinner?().address?.zipcode

Function shortcuts

()->  // equivalent to function(){}

Sexy function calling

func 'arg1','arg2' // equivalent to func('arg1','arg2')

There is also multiline comments and classes. Obviously you have to compile this to javascript or insert into the page as <script type='text/coffeescript>' but it adds a lot of functionality :) . Using <script type='text/coffeescript'> is really only intended for development and not production.

这篇关于在JavaScript中是否存在null-coalescing(Elvis)运算符或安全导航运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆