如何将一行咖啡脚本代码制作成多行代码?
How to make one line coffee script code to multiline?
我最近尝试使用 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")
所以第一行很长,我想把它写成多line.Maybe像这样的代码要好得多,但正如你所知,这在咖啡脚本中是错误的。
# 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
我最近尝试使用 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")
所以第一行很长,我想把它写成多line.Maybe像这样的代码要好得多,但正如你所知,这在咖啡脚本中是错误的。
# 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