尝试在JavaScript中复制引用传递行为:如何适当地修改我的代码
Trying to replicate pass-by-reference behavior in JavaScript: How to modify my code appropriately
所以我有两个函数,我希望它们能一起工作,在div周围反弹一个对象。我使用的是图形库,所以下面会有一些不熟悉的代码。我想你仍然会理解我要做的事情的要点
function homepage_screensaver()
{
/*
Create Raphael object in the space of the div with id "homeandidiv"
*/
var pappos = $("#homeanidiv").position();
var papx = pappos.left;
var papy = pappos.top;
var papheight = $("#homeanidiv").height();
var papwidth = $("#homeanidiv").width();
var paper = Raphael(papx, papy, papwidth, papheight);
/*
paper: space in which the circle will be bound
*/
var circx = Math.floor(Math.random() * Number.MAX_VALUE) % papwidth;
var circy = Math.floor(Math.random() * Number.MAX_VALUE) % papheight;
/*
circx, circy: initial positions of circle on the paper
*/
var mycirc = paper.circle(circx, circy, 10);
mycirc.attr("fill","#F9C624");
var theta = Math.floor(Math.random() * Number.MAX_VALUE) % 4 + 1;
/*
theta = 1 <---> object moving at a 45-degree angle
theta = 2 <---> object moving at a 135-degree angle
theta = 3 <---> object moving at a 225-degree angle
theta = 4 <---> object moving at a 315 degree angle
*/
var circwrapper = new Array(mycirc, theta);
window.setInterval(function() { move_obj(circwrapper, papwidth, papheight);}, 100);
}
function move_obj(obwrap, backwidth, backheight)
{
var ob = obwrap[0]; // object
var th = obwrap[1]; // theta, the current direction of the object
var BB = ob.getBBox(); // bounding box for object
var dx = 0;
var dy = 0;
if (BB.x >= backwidth && (th == 1 || th == 2))
dx = -1;
else if (BB.x <= 0 && (th == 3 || th == 4))
dx = 1;
if (BB.y >= backheight && (th == 2 || th == 3))
dy = -1;
else if (BB.y <= 0 && (th == 1 || th == 4))
dy = 1;
ob.transform("T " + dx + ", " + dy);
if (dx == 1 && dy == -1)
th = 1;
else if (dx == 1 && dy == 1)
th = 2;
else if (dx == -1 && dy == 1)
th = 3;
else // (dx == -1 && dy == -1)
th = 4;
obwrap[0] = ob;
obwrap[1] = th;
}
以下是我在测试页面后意识到的问题:我的函数move_obj(...)
实际上并没有影响我传递给它的第一个参数
obwrap[0] = ob;
obwrap[1] = th;
表示我正在尝试实际修改作为第一个参数传入的数组的值。
我的问题有什么"快速解决办法"吗?我宁愿不回去尝试把事情变成全局变量。
正如你所知,我研究了JS中的引用传递问题,这里说数组是通过引用传递的:http://orizens.com/wp/topics/javascript-arrays-passing-by-reference-or-by-value/.所以我看不出这里出了什么问题。
您必须在"move"函数返回后进行重新分配。
window.setInterval(function() {
var wrapper = [mycirc, theta];
move_obj(wrapper, papwidth, papheight);
mycirc = wrapper[0];
theta = wrapper[1];
}, 100);
将新值分配给数组有效,但它只影响数组。构建数组时,您正在对这两个变量的值进行复制。数组槽和变量之间没有后续的隐式关系,因此对数组的更改对自变量的值没有影响。
相关文章:
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何动态插入jquery代码
- 如何在php变量中嵌入JQuery代码
- 代码不会验证
- 如何从rails中的代码中删除新行( )
- Angularjs代码未在匿名函数中运行
- 如何做到这一点,使代码在不传递条件后执行函数
- 如何在读取XLS/XLSX本地文件时,使用IE的javascript代码启用未标记为安全的ActiveX控件
- 面向对象的Javascript代码在IE7中不起作用
- 使用服务器端代码从客户端提供的不可靠的API获取JSON输出,并以适当的格式在页面上呈现数据
- 将确定缩放图像的适当尺寸的代码
- 在这段代码中,1/cos(x)对于一款3d画布游戏有何意义?
- 尝试在JavaScript中复制引用传递行为:如何适当地修改我的代码
- 通过编写适当的 JavaScript 函数来简化我的代码