理解jBox在数组中访问属性

understanding jBox accessing properties in a array

本文关键字:访问 属性 数组 jBox 理解      更新时间:2023-09-26

我正在检查jBox.js的代码,遇到了下面的代码片段:

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);

现在我的问题是关于一个非常复杂的代码行,它试图访问数组中的某个属性,我说的是下面这行代码:

this.images[gallery][id].src

上面这行代码真正要访问的是哪种数组?我已经工作和访问数组如下:

var s = [{
  a : 'name',
  b : 'surname'
}];
val = s[0].a; // "name"
console.log(val);

但是我突出显示的语法似乎有一个额外的层次结构。我很抱歉,我仍然是一个javascript新手,我发现很难想象如何像下面这样访问数组。

this.images[gallery][id].src

看起来像?有人能给我举个例子吗?解释一下?

谢谢。

gallery, id可能只是字符串,您可以使用以下属性访问器:

var gallery = 'galleryx',
    id      = 'idx';
var images = { 'galleryx': { 'idx': 2 } };
console.log(images[gallery][id]) // === 2