我有一个非常奇怪的问题。我正在尝试访问一个子对象,但它就是行不通。最简单的方法是只向您展示 FireBug 的控制台输出:
console.log(DesignDocument)
DesignDocument: mwrNsBrowser
html: e.fn.e.init[1]
name: "Game Design Document"
rootName: "Game Design Document"
__proto__: Object
afterLoadCategory: function (bitData, catName)...
bits: Object
hide: function ()...
html: ""
loadCategory: function (catName, parentBit)...
name: ""
rootName: ""
show: function ()...
tmp: Object
现在我正在尝试访问原型(prototype)中定义的“bits”对象。也能正常工作。
console.log(DesignDocument.bits)
Object
DocumentSet1: Array[1]
0: mwrBrowserBit
length: 1
但是问题来了。我尝试获取“DocumentSet1”数组:
var selector = "DocumentSet1";
console.log(DesignDocument.bits[selector])
undefined
它只返回“undefined”! 我目前不知道为什么这行不通。希望有人能告诉我我太笨了...
最佳答案
数组 DocumentSet1 是 DesignDocument.bits 对象的一个属性,因此将其记录到控制台的正确语法是:
console.log(DesignDocument.bits.DocumentSet1);
或者如果你想显示数组中的特定项目:
console.log(DesignDocument.bits.DocumentSet1[num]);
关于javascript - 无法访问对象属性(奇怪,因为它应该像往常一样工作),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10414820/