CoffeeScript 未定义 [英] CoffeeScript Undefined

查看:22
本文介绍了CoffeeScript 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中检查变量是否从未创建,我们只是这样做

In javascript to check if a variable was never created, we just do

if (typeof MyVariable !== "undefined"){ ... }

我想知道我是如何在咖啡脚本中做到这一点的?...我尝试类似

I was wonder how I do that in coffeescript?... I try something like

if (MyVariable?false){ ... }

但是这个检查如果 MyVariable 是一个函数,如果是这样,它将调用 MyVariable(false),如果不是,它将调用 void(0) 或类似的东西.

but this check if MyVariable is a function if so that will call MyVariable(false) if not that will call void(0) or something like that.

推荐答案

最后我找到了这个简单的方法:

Finally I found this easy way to do it:

if (MyVariable?){ ... }

这将产生:

if (typeof MyVariable !== "undefined" && MyVariable !== null){ ... }

2014 年 4 月 7 日更新演示链接

这篇关于CoffeeScript 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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