0

私はこれをやろうとします:

posts[fbposts[fbpost].id] = ({
    name: fbposts[fbpost].from.name,
    link: "http://www.facebook.com/" + fbposts[fbpost].from.id,
    img: "http://graph.facebook.com/" + fbposts[fbpost].from.id + "/picture",
    message: fbposts[fbpost].message,
    to: ({
        name: name,
        link: link,
    }),
    created: timeDifference(Date.parse((fbposts[fbpost].updated_time))),
    sortvar: (Date.parse(fbposts[fbpost].updated_time)),
    comments: ({
        /* looping through the comments*/
        if (fbposts[fbpost].comments.count) {
            for (var comment in fbposts[fbpost].comments.data) {
                name: fbposts[fbpost].comments.data[comment].from.name;
                link: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id;
                img: "http://www.facebook.com/" + fbposts[fbpost].comments.data[comment].from.id; + "/picture";
                message: fbposts[fbpost].comments.data[comment].message;
                created: timeDifference(Date.parse(fbposts[fbpost].comments.data[comment].created_time));
            }
        }
    })
});
}

しかし、明らかにコメントにたどり着くと、オブジェクトの宣言内でループを実行したり、オブジェクトの宣言内で条件文を実行したりできないため、スクリプトがブレーキをかけます。代わりにこれをどのように行う必要がありますか?

4

2 に答える 2

1

必要な引数を渡す関数を作成し、それを次のcommentsようなプロパティに使用する必要があります。

comments: (/* return value from your function */)
于 2012-06-19T20:43:05.690 に答える
1

実際には、クロージャーを使用してこれを行うことができます...

comments: function() { ... looping code ...; return data}(),

これが行うことは、インライン (1 回使用) 関数を定義してすぐに呼び出し、結果として有効な JavaScript を返すことです。

var x = {
    ten: 10, 
    thirty: function(){var a = 10; a += 20; return a}(), 
    fifty: 25+25};

x は次のようなオブジェクトになりました。

ten: 10
thirty: 30
fifty: 50
于 2012-06-19T20:43:53.787 に答える