相关疑难解决方法(0)

咖啡脚本测试,如果没有定义

根据咖啡脚本网站

console.log(s) if s?
Run Code Online (Sandbox Code Playgroud)

应该生成

if (typeof s !== "undefined" && s !== null) {
    console.log(s);
}
Run Code Online (Sandbox Code Playgroud)

但是我的浏览器中出现的是什么

  if (s != null) {
      return console.log(s);
  }
Run Code Online (Sandbox Code Playgroud)

使用coffee-script-source(1.6.2),coffee-rails(3.2.2),rails-backbone(0.7.2),rails(3.2.13)

这是我的咖啡脚本功能.任何想法为什么我没有得到什么咖啡脚本网站说我应该?

window.p = (s) ->
    console.log(s) if s?
Run Code Online (Sandbox Code Playgroud)

coffeescript

14
推荐指数
1
解决办法
2万
查看次数

coffeescript不为null或未定义的行为

所以我正在玩咖啡脚本并得到这种行为:

a?将转化为typeof a !== "undefined" && a !== null;- 那没关系

问题是如果我尝试相同的a.foo?.这次它转化为a.foo != null; 我得到的相同结果a[foo].

是否有"快速"的方法来检查是否a.foo为null或未定义,如有?

javascript coffeescript existential-operator

12
推荐指数
0
解决办法
9432
查看次数