外观
Today, in codes for a BINGO game, I saw a JavaScript code snippet as below.
If (evt) {
var thisSquare = evt.target;
}
else {
var thisSquare = window.event.srcElement;
}You may want to ask what "thisSquare" here means. A good question! You know, typically, a BINGO game is played on a 5 rows * 5 columns layout (therefore there are 25 cells in total). The above code is aimed to fetch the information that which cell is clicked by the mouse (so you now know "thisSquare" refers to the currently clicked cell). However, the meaning of "thisSquare" is not my concern. I paid much attention to the contents after the equality sign, i.e. "evt.target" and "window.event.srcElement".
Actually, the above code snippet can be written by a ternary if operator employing the inline if syntax as below:
var thisSquare = (evt) ? evt.target : window.event.srcElement;Note: evt is not a boolean. If evt isn't null, underfined, NaN (not sure), 0 or false, it's considered truthy. Basically, if evt has been set already. it's truthy.