How do I use the CoffeeScript existential operator to check some object properties for undefined?

前端 未结 3 1887
面向向阳花
面向向阳花 2020-12-02 15:56

I would like to use the CoffeeScript existential operator to check some object properties for undefined. However, I encountered a little problem.

Code like this:

3条回答
  •  没有蜡笔的小新
    2020-12-02 16:21

    Wild guess; have you tried console.log test.test if test?.test??

    Just tested it with coffee -p -e 'console.log test.test if test?.test?', which compiles to:

    (function() {

    if ((typeof test !== "undefined" && test !== null ? test.test : void 0) != null) { console.log(test.test); }

    }).call(this);

提交回复
热议问题