oco*_*odo 2 undefined coffeescript
检查是否定义了简单变量是很容易的 myVar?
问题是,CoffeeScript中是否有一种很好的方法来检查是否定义了嵌套属性?
例如
if property.p.p.p.p?
alert "Hello"
Run Code Online (Sandbox Code Playgroud)
抛出ReferenceErrorif property.p.p.p(或property.p.p| property.p)未定义.
这只是我们必须递归检查的东西,还是有一个很好的功能?
Ben*_*ick 12
if property?.p?.p?.p?.p?
alert "Hello"
Run Code Online (Sandbox Code Playgroud)
你想要的是什么
转化为
var _ref, _ref1, _ref2;
if ((typeof property !== "undefined" && property !== null ? (_ref = property.p) != null ? (_ref1 = _ref.p) != null ? (_ref2 = _ref1.p) != null ? _ref2.p : void 0 : void 0 : void 0 : void 0) != null) {
alert("Hello");
}
Run Code Online (Sandbox Code Playgroud)
在JS中.
相关的文档:
存在运算符的存取变体?可用于在属性链中吸收空引用.使用它代替点访问器.在基值可能为null或未定义的情况下.如果存在所有属性,那么您将获得预期结果,如果链断开,则返回undefined而不是将引发的TypeError.
| 归档时间: |
|
| 查看次数: |
871 次 |
| 最近记录: |