1

I was looking at the most recent code for highcharts, when I saw this segment:

wrap(opacityHook, 'get', function (proceed, elem, computed) {
    return elem.attr ? (elem.opacity ||Â 0) : proceed.call(this, elem, computed);
});

Why is there an  character in the source code? That couldn't be right, could it?

4

1 回答 1

1

elem.opacity在这种情况下,当所述元素属性 ( ) 为falsey时,看起来这是一种快速抛出异常的方法。

运行以下语句会引发“运行时”错误:

alert(elem.opacity ||Â 0);

比写作更短的写作:

alert(elem.opacity ? elem.opacity : throw 'some error message');

注意:该throw语句不会按预期工作(向控制台写入“一些错误消息”)——因为三元表达式应该返回一个值。

因此,(elem.opacity ||Â 0)足以并保持代码简洁/简洁和防御性,但不是很语义......


简而言之

以下:

... (elem.opacity ||Â 0) ...

转换为:

elem.opacity如果是 则抛出错误undefined

于 2013-07-16T18:52:01.727 回答