I've been looking at someone else's code and I'm stuck on some syntax that I can't make complete sense of.
var $windows = $('#tgx-window,#tgs-window,#tgm-window,#tgl-window'),
$buttons = $('#tgx-button,#tgs-button,#tgm-button,#tgl-button');
$windows.hide();
$buttons.on('click', function() {
var $id;
$buttons.removeClass('closebutton');
$id = $('#' + this.id.split('-')[0] + '-window');// Get window id
// alert($id);
$windows.slideUp();
if(! $id.is(':visible') ) {
$id.slideDown();
$(this).addClass('closebutton');
}
});
The line I'm looking at is $id = $('#' + this.id.split('-')[0] + '-window');
I'm trying to make sense out of the index array value: [0] and why it's there at all.
According to the ECMAScript 5 specification, using the split() method "Returns an Array object". So the substring in question is being held at the index value [0] (even though there aren't any more values in the array (I assume).
Then we append "-window" and get our final value: $id = #tgx-window (if you clicked the #tgx-button value).
Is this correct? It's really clever imo. But I don't understand why split and an array value are being used together.
Also, I was trying to use alert(); calls directly after the $id statement to try and "look into what was being created" but I didn't find any useful information. Mostly [object Object] and [object HtmlDivElement] which didn't give me any ah-ha moments. What would you have done to glean some clarity from what was happening in the line: $id = $('#' + this.id.split('-')[0] + '-window');// Get window id ??
If you read this far, thank you so much. I haven't had this much fun looking at code in years. It's an awesome feeling :)
.splitalways returns an array. So you have to access the element of the array that you want to get (0in this case).alert($id.attr('id'));try this.console.log($id).