在javascript中传递引用
这是我的第一篇SO帖子。我永远感谢这个社区所拥有和分享的信息。谢谢 我来自Flash,我甚至不确定该问什么问题。我所能做的就是展示我的代码示例,然后解释我正在尝试做什么。我不完全理解我在这里试图说明的术语,所以我觉得最好省略它们 下面的代码不完整,因为它只包含我认为与我的问题相关的部分。请参考我的代码中的注释以查看我的问题 编辑:此处的完整源文件:[链接已删除]console.log输出有问题的问题在javascript中传递引用,javascript,object,inheritance,Javascript,Object,Inheritance,这是我的第一篇SO帖子。我永远感谢这个社区所拥有和分享的信息。谢谢 我来自Flash,我甚至不确定该问什么问题。我所能做的就是展示我的代码示例,然后解释我正在尝试做什么。我不完全理解我在这里试图说明的术语,所以我觉得最好省略它们 下面的代码不完整,因为它只包含我认为与我的问题相关的部分。请参考我的代码中的注释以查看我的问题 编辑:此处的完整源文件:[链接已删除]console.log输出有问题的问题 <script type="text/javascript"> v
<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>
var a_箱=[];
var-chestID=0;
//我正在创建一个插件,可以创建多个实例
(函数($){
$.fn.chestPlugin=函数(选项){
//此函数用于创建一个主精灵对象,我的许多精灵都将使用该对象
//我已经简化了这些特性,以触及问题的核心
var DHTMLSprite=函数(参数){
变量标识=参数标识,
var={
getID:function(){
返回标识;
}
};
归还;
};
//ChestSprite继承DHTMLSprites属性,然后添加一些自己的属性
var chestSprite=函数(参数){
变量标识=参数标识,
即=DHTMLSprite(参数);
that.reveal=函数(){
控制台日志(ident);
};
归还;
};
//在这里,我创建了多个箱子实例
var宝藏=功能($drawTarget,箱子){
对于(i=0;i当您写入:
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};
})();
}
for(i=0;i您忘记将'that'声明为局部变量,因此在每次迭代中都会覆盖它
var chestSprite = function(params) {
var that;
var animInterval;
...
感谢您的查找。索引1实际上应该返回“chest1”。chestID从零开始,在启动chestSprite后递增。如果我使用索引0,它仍然会跟踪“chest2”。我需要一种方法来维护对每个单独的chestSprite的引用。查看完整代码时,您会看到:ident=params.ident行后有一个逗号。我假设var带到“that”,对吗?那是真的,除了中间有一个分号:var x=params.x,y=params.y,contents=params.contents,initIndex=10;animiindex=initIndex,很好!!我简直不敢相信我为此浪费了两天时间…我对你抓到这一点印象深刻。这只是在浏览代码,还是你用了什么来验证它。尝试看看我是否能从这个错误中获得更多的经验。:)我刚刚看到了,但是有工具可以做。闭包编译器有一个undefinedVars标志。@user1780145:你可以使用类似的工具来捕捉类似的东西。另外,它会让浏览器告诉你(在支持它的浏览器上)。严格模式还有其他好处。原来它只是一个分号。我真的很感谢你花时间提供答案。