如何将一行咖啡脚本代码转换为多行

How to make one line coffee script code to multiline?

本文关键字:脚本 代码 转换 咖啡 一行      更新时间:2023-09-26

我最近尝试使用coffee script,但我有一些这样的代码

if userAgent.match(/iPad/i) or userAgent.match(/iPhone/i) or userAgent.match(/iPoid/i) or userAgent.match(/Android/i) or userAgent.match(/IEMobile/i) or userAgent.match(/BlackBerry/i)
    console.log("This is a mobile device")
else
    console.log("This is not mobile device")

所以第一行很长,我想让它多行。也许像这样的代码要好得多,但如您所知,这在咖啡脚本中是错误的。

# This is the wrong code
if userAgent.match(/iPad/i) 
    or userAgent.match(/iPhone/i) 
    or userAgent.match(/iPoid/i) 
    or userAgent.match(/Android/i) 
    or userAgent.match(/IEMobile/i) 
    or userAgent.match(/BlackBerry/i)
    console.log("This is a mobile device")
else
    console.log("This is not mobile device")

或者像这样的代码:

# This is also wrong
if userAgent.match(/iPad/i) or 
  userAgent.match(/iPhone/i) or 
  userAgent.match(/iPoid/i) or 
  userAgent.match(/Android/i) or 
  userAgent.match(/IEMobile/i) or 
  userAgent.match(/BlackBerry/i)
    console.log("This is a mobile device")
else
    console.log("This is not mobile device")

那么有什么方法可以纠正这一点吗?

另外,如果你像这样编写正则表达式,你会好得多:

if userAgent.match /iPad|iPhone|iPod|Android|IEMobile|BlackBerry/i