简体   繁体   中英

understanding jBox accessing properties in a array

I was just going through the code of jBox.js and came across the following snippet:

var appendImage = function(gallery, id, preload, open) {
  if (jQuery('#jBox-image-' + gallery + '-' + id).length) return;

  var image = jQuery('<div/>', {
    id: 'jBox-image-' + gallery + '-' + id,
    'class': 'jBox-image-container'
  }).css({
    backgroundImage: 'url(' + this.images[gallery][id].src + ')',
    backgroundSize: this.options.imageSize,
    opacity: (open ? 1 : 0),
    zIndex: (preload ? 0 : this.imageZIndex++)
  }).appendTo(this.content);

  var text = jQuery('<div/>', {
    id: 'jBox-image-label-' + gallery + '-' + id,
    'class': 'jBox-image-label' + (open ? ' active' : '')
  }).html(this.images[gallery][id].label).appendTo(this.imageLabel);

  !open && !preload && image.animate({opacity: 1}, this.options.imageFade);
}.bind(this);

now my question is pertaining to a really complicated line of code that is trying to access a certain property in a array, i am talking about the below line of code:

this.images[gallery][id].src

what kind of an array is the above line really trying to access ? I have worked and accessed arrays like below:

var s = [{
  a : 'name',
  b : 'surname'
}];

val = s[0].a; // "name"

console.log(val);

But the syntax i have highlighted seems to have a extra bit of hierarchy. I am sorry, i am still a javascript novice and i am finding it hard to visualize how a array that gets accessed like below.

this.images[gallery][id].src

Would look like ? So well can somebody give me an example ? and explain ?

Thank you.

gallery, id might just be strings and you can use the following property accessor for those:

var gallery = 'galleryx',
    id      = 'idx';

var images = { 'galleryx': { 'idx': 2 } };

console.log(images[gallery][id]) // === 2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM