3

这是我的第一个 SO 帖子。我永远感激这个社区拥有和分享的信息。谢谢。

我来自 Flash,我什至不确定要问的正确问题是什么。我所能做的就是列出我的代码示例,然后解释我想要做什么。我没有完全理解我在这里试图说明的术语,所以我觉得最好省略它们。

下面的代码不完整,因为它只包含我认为与我的问题相关的部分。请参阅我的代码中的注释以查看我的问题。

编辑:此处的完整源文件:[链接已删除] console.log 输出有问题的问题。

    <script type="text/javascript"> 
    var a_chests = [];
    var chestID = 0;

    //I'm creating a plugin to be able to make multiple instances
    (function ($) {
        $.fn.chestPlugin = function (option) {
            //This function creates a master sprite object which many of my sprites will use
            //I've simplified the features to get to the heart of my question
            var DHTMLSprite = function (params) {
                var ident = params.ident,
                var that = {
                    getID: function(){
                        return ident;
                    }
                };
                return that;
            };

            //ChestSprite inherits DHTMLSprites properties and then adds a few of its own
            var chestSprite = function(params) {
                var ident = params.ident,
                that = DHTMLSprite(params);
                that.reveal=function(){
                    console.log(ident);
                };

                return that;
            };

            //Here I create multiple instances of the chests
            var treasure = function ( $drawTarget,chests) {
                for (i=0;i<chests;i++){
                    var cs = chestSprite({
                        ident: "chest"+chestID
                    })
                    console.log(cs.reveal()) 
                    //This logs "chest0", "chest1", "chest2" as the for loop executes
                    //This behavior is correct and/or expected!

                    a_chests[chestID]={id:i,ob:cs};
                    //I add a reference to the new chestSprite for later

                    chestID++;
                    //increment the chestID;
                }
                console.log(a_chests[1].ob.reveal());
                //This always logs "chest2" (the last chest that is created), even though
                //the logs in the for loop were correct. It seems it is referencing the
                //DHTML object (since the DHTMLSprite function returns that;) and since 
                //there is no reference to which chest I need, it passes the last one.

                //Is there any way I can pass a reference to DHTMLSprite in order to retain
                //the reference to the three individual chests that are created?

                //Is there another solution altogether? Thanks!!!
            };

            //The rest of the code.
            return this.each(function () {
                var $drawTarget = $(this);
                treasure($drawTarget,3);
            });
        };
        })(jQuery);


        </script>
4

2 回答 2

2

您忘记将“that”声明为局部变量,因此每次迭代都会覆盖它。

    var chestSprite = function(params) {
      var that;
      var animInterval;
      ...
于 2012-10-28T02:32:55.983 回答
0

当你写:

a_chests[chestID]={id:i,ob:cs};

您正在分配 cs 对象本身,而不是该对象的实例。如果稍后您修改 cs,这也会修改您存储在 ob 属性中的内容。

我想你需要的是一个闭包

for (i=0;i<chests;i++){
(function(){
    var cs = chestSprite({ident: "chest"+chestID});
    a_chests[chestID]={id:i,ob:cs};
})();
}

这样,每个循环都会创建一个不同的 cs 对象。

于 2012-10-28T01:15:18.823 回答