【发布时间】:2023-03-03 05:19:27
【问题描述】:
在让“this”按我预期的方式运行时遇到了一些麻烦 -
基本上,我有一个对象,但我无法从同一对象中的函数访问对象中的数组 -
看起来像这样:
var articles = {
article : {
1: {
title : 'This is a Title',
content : 'This is the content of the article'
},
2: {
title : 'This is the second article',
content : 'This is the second article content'
},
3: {
title : 'This is the third article',
content : 'Making information transferrable. Identifiable. Manipulatable.'
}
},
numArticles : function(){
var size = 0, key;
for (key in this.article){
if(this.article.hasOwnProperty(key)) size++;
}
return size;
},
buildInterface : function(){
var aSize = this.numArticles();
for(var i = 0; i < aSize; i++){
$('body').append('<article><h2>' + this.article[i].title + '</h2></article>');
}
}
}
buildInterface() 函数在这种情况下无法访问 'article' 数组。
这是一个正在进行的示例:
http://jsfiddle.net/merk/xV2n6/41/
如有任何帮助,我们将不胜感激 -
我有一种预感,这可能是一个范围界定问题 - 希望它与 JSFiddle 无关 -
非常感谢 -
和平
标记
【问题讨论】:
-
@thatidiotguy 这是变量“articles” ...
-
@Pointy:我认为那个白痴的意思是“我没有看到一个数组”:p j/k
-
@jAndy 为我的白痴道歉。不过我确实有这个名字!
-
@thatidiotguy:哈哈,是的,我只是在开玩笑,看起来很像一个错字
-
另外,既然您将
article视为一个数组,为什么不把它变成一个实际的数组呢?您将免费获得length,因此您不需要 numArticles。
标签: javascript arrays object indexing