开发者

Question about this object literal

开发者 https://www.devze.com 2023-01-15 14:33 出处:网络
What happens when returnThis.label is ref开发者_如何学Pythonerenced? Can one give me an example of how this might be used?

What happens when returnThis.label is ref开发者_如何学Pythonerenced? Can one give me an example of how this might be used?

returnThis = {
        'label' : aLabel ? this.value : false
        };


This makes use of ternary syntax.

aLabel ? this.value : false means: if aLabel is truthy (true, 1, "a", etc.), evaluate to this.value. Otherwise, evaulate to false.

The code is equivalent to the following:

returnThis = {};
if(aLabel) {
    returnThis.label = this.value;
} else {
    returnThis.label = false;
}


Nothing happens (it just gets the value). The statement: aLabel ? this.value : false has already been executed.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号