簡體   English   中英

了解jBox訪問數組中的屬性

[英]understanding jBox accessing properties in a array

我只是瀏覽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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM