$('#toggle-linecolor').click(function () {
chart.yAxis[0].update({
lineColor: lineColor
});
lineColor = { red: 'blue', blue: 'red' }[lineColor];
});
答案 0 :(得分:3)
它在'red'
和'blue'
之间切换。
鉴于你开始:
var lineColor = 'red';
执行此操作会将lineColor
更改为“蓝色”
lineColor = { red: 'blue', blue: 'red' }[lineColor];
// The property [red] of that object has a value of 'blue'
lineColor // <--- 'blue'
再次执行它,它会将值切换为'red'
lineColor = { red: 'blue', blue: 'red' }[lineColor];
// The property [blue] of that object has a value of 'red'
lineColor // <---- 'red'
答案 1 :(得分:0)
它交换/切换颜色。红色 - &gt;蓝色,蓝色 - &gt;红色
lineColor = 'blue';
lineColor = { red: 'red', blue: 'blue' }[lineColor]; // 'red'
与
相同lineColor = { red: 'red', blue: 'blue' }.blue; // 'red'
答案 2 :(得分:0)
这是一种巧妙的方式来实现切换作为一个班轮。你可以像这样解压缩它:
var lineColor = 'red';
function toggleColor(){
chart.yAxis[0].update({
lineColor: lineColor
});
var opposites = { red : 'blue', blue : 'red' };
if( lineColor == 'red' ){
lineColor = opposites.red; // assigns 'blue' to lineColor
}
else if( lineColor == 'blue' ){
lineColor = opposites.blue; // assigns 'red' to lineColor
}
// but you can shorten the above if/else statement to:
lineColor = opposites[ lineColor ];
// which can be shortened to this if you don't want to
// assign the opposites object to a variable:
lineColor = { red: 'blue', blue: 'red' }[lineColor];
}
编辑注释:您可以使用两种以上的颜色创建循环序列:
opposites = { red : 'green', green : 'yellow', yellow : 'blue', blue : 'red' };
所以如果当前颜色为红色变为绿色,如果绿色变为黄色,如果黄色变为蓝色,如果变为蓝色变为红色:http://jsfiddle.net/p3bw04vw/