2

让“这个”表现得像我预期的那样有些困难——

基本上,我有一个对象,我无法从同一个对象中的函数访问对象中的数组 -

它看起来像这样:

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() 函数无法访问“文章”数组。

这是一个正在进行的示例:

http://jsfiddle.net/merk/xV2n6/41/

任何帮助将不胜感激 -

我有一种预感,这可能是一个范围界定问题——希望它与 JSFiddle 无关——

万分感谢 -

和平

标记

4

2 回答 2

3

您的article变量索引不一致:属性是从 开始定义的,1但您从方法循环开始。你可以用...解决这个问题0buildArticlesfor

for(var i = 1; i <= aSize; i++){
  $('body').append('<article><h2>' + this.article[i].title + '</h2></article>');               
};

...或者(这对我来说更好,因为您基本上是在尝试使用 Object 来完成数组的工作)将article定义重写为适当的数组:

article : [{
        title : 'This is a Title',
        content : 'This is the content of the article'
    }, {
    title : 'This is the second article',
    content : 'This is the second article content'   
    }, {
    title : 'This is the third article',
    content : 'Making information transferrable. Identifiable. Manipulatable.'   
    }],
...

... 让您的 buildArticlesfor循环保持原样(因为索引现在正确地从 0 开始)。

顺便说一句,这样你甚至不必创建一个特殊的功能来计算你的文章:article.length就足够了。

这是带有这种方法的插图的JS Fiddle 。


作为旁注,如果您实际上检查了调试器,您会注意到它this.articles[0]就是undefined(因此试图title摆脱它是错误的),而不是this.articles. 因此,这绝对不是范围的问题。

于 2012-11-24T19:49:06.497 回答
0

这应该有效:

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 = 1; i <= aSize; i++){
            console.log( '<article><h2>' + this.article[i]['title'] + '</h2></article>');               
        };
    }
}

您的问题是,您从数组中零位置的元素开始。但是你没有 0 元素。所以它会给你一个错误。这正在按预期工作。

于 2012-11-24T19:53:45.227 回答