我有一个最奇怪的问题,我一辈子都想不出来。如果我这么做:
console.log(settings);我明白了:
Object{
activeImage: 0
containerBorderSize: 10
containerResizeSpeed: 400
fixedNavigation: false
imageArray: Array[58]
imageBlank: "http://www.cappellaniauniromatre.org/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-blank.gif"
imageBtnClose: "http://www.cappellaniauniromatre.org/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-btn-close.gif"
imageBtnNext: "http://www.cappellaniauniromatre.org/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-btn-next.gif"
imageBtnPrev: "http://www.cappellaniauniromatre.org/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-btn-prev.gif"
imageLoading: "http://www.cappellaniauniromatre.org/wp-content/plugins/nextgen-gallery/products/photocrati_nextgen/modules/lightbox/static/jquery.lightbox/lightbox-ico-loading.gif"
keyToClose: "c"
keyToNext: "n"
keyToPrev: "p"
overlayBgColor: "#000"
overlayOpacity: 0.8
txtImage: "Image"
txtOf: "of"
__proto__: Object
}现在,如果我这样做的话:
console.log(settings.imageArray);我得到了一个空数组!
[]我知道这个数组有58个元素,如果我直接访问属性,为什么它显示为空呢?如果我试图直接访问任何其他属性,就会得到正确的值。但是,如果我试图访问"imageArray",就会得到一个空数组。为什么会这样?
发布于 2013-11-17 23:22:16
console.log(settings)将对settings的实时引用放入控制台。这意味着settings在console.log(settings)之后的任何更改都将出现在控制台中。
例如:
var settings = { imageArray: [ ] };
console.log(settings);
console.log(settings.imageArray);
settings.imageArray = [ 'where', 'is', 'pancakes', 'house?' ];
console.log(settings.imageArray);将在控制台中给出如下内容:
Object
imageArray: Array[4]
[]
["where", "is", "pancakes", "house?"]第一个console.log(settings.imageArray)是空的,因为settings.imageArray引用被替换了。
演示:http://jsfiddle.net/K4BEs/
https://stackoverflow.com/questions/20037459
复制相似问题