javascript - 如何将一行 CoffeeScript 代码制作成多行代码?

标签 javascript coffeescript

我最近尝试使用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/

相关文章:

javascript - 在函数内按时间间隔触发函数(coffeescript)

node.js - 带有 npm 启动脚本的 Nodejs 应用程序

javascript - Google Maps API V3 绘图地址

coffeescript - 如何在 CoffeeScript 中覆盖父方法,同时仍然能够调用父方法

javascript - javascript 中是否有等效的 python strip 函数?

javascript - AngularJS 和 Ionic : Two Objects in one Scope?

javascript - 如何在浏览器中使用 knex.js

javascript - 样式化浏览器对话框

逐行调试 CoffeeScript

php - 从 PHP 回发响应到 javascript