我最近尝试使用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")
所以第一行很长,我想把它变成多行。也许像这样的代码要好得多,但正如你所知,这在 CoffeeScript 中是错误的。
# 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
关于javascript - 如何将一行 CoffeeScript 代码制作成多行代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28227925/