在javascript中传递引用

时间:2012-10-28 00:47:44

标签: javascript object inheritance

这是我的第一篇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>

2 个答案:

答案 0 :(得分:2)

你忘了将'that'声明为局部变量,因此在每次迭代时都会被覆盖。

    var chestSprite = function(params) {
      var that;
      var animInterval;
      ...

答案 1 :(得分: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对象。