【问题标题】:Using 'this' in a Javascript Object在 Javascript 对象中使用“this”
【发布时间】: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


【解决方案1】:

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

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.'   
    }],
...

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

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

这里是JS Fiddle,带有这种方法的说明。


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

【讨论】:

  • 好酷-我会试一试-感谢您的反馈-
  • 关于正确的 Array 提示的好建议 - 肯定会切换到那个 -
【解决方案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 = 1; i <= aSize; i++){
            console.log( '<article><h2>' + this.article[i]['title'] + '</h2></article>');               
        };
    }
}

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

【讨论】:

    猜你喜欢
    • 2017-05-06
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    相关资源
    最近更新 更多