简体   繁体   English

尝试在JavaScript中复制通过引用行为:如何适当地修改我的代码

[英]Trying to replicate pass-by-reference behavior in JavaScript: How to modify my code appropriately

So I have 2 functions that I was hoping would work together to bounce an object around a div. 因此,我希望有2个函数可以共同使div周围的对象反弹。 I'm using a graphics library, so there will be some unfamiliar pieces of code below. 我使用的是图形库,因此下面会有一些不熟悉的代码。 I think you'll still understand the gist of what I'm trying to do. 我认为您仍然会了解我要做什么。

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;
}

Here's the problem that I've realized after testing my page: my function move_obj(...) is not actually affecting the first parameter I'm passing to it. 这是测试页面后我意识到的问题:我的函数move_obj(...)实际上并没有影响我传递给它的第一个参数。 You can see at the end of my function that I have 您可以在功能结束时看到

  obwrap[0] = ob;
  obwrap[1] = th;

indicating that I'm trying to actually modify values of the array that is passed in as the first parameter. 指示我正在尝试实际修改作为第一个参数传入的数组的值。

Is there any "quick fix" to my problem? 我的问题是否有“快速解决方案”? I would prefer not to go back and try to make things global variables. 我宁愿不回去尝试使事物成为全局变量。

Just so you know, I have researched the issue of passing by reference in JS and here it says that arrays are passed by reference: http://orizens.com/wp/topics/javascript-arrays-passing-by-reference-or-by-value/ . 大家知道,我研究了JS中按引用传递的问题,在这里它说数组是按引用传递的: http : //orizens.com/wp/topics/javascript-arrays-passing-by-reference-or -按值/ So I don't see what's going wrong here. 所以我看不出这里出了什么问题。

You have to do the reassignment after the "move" function returns. “移动”功能返回后,您必须进行重新分配。

window.setInterval(function() {
  var wrapper = [mycirc, theta];

  move_obj(wrapper, papwidth, papheight);

  mycirc = wrapper[0];
  theta = wrapper[1];
}, 100);

Assigning new values to the array works but it only affects the array. 分配新值阵列工作 ,但它仅影响该阵列。 When you build the array, you're making copies of the values of the two variables. 构建数组时,您正在复制两个变量的值。 There's no subsequent implicit relationship between the array slots and the variables, so changes to the array have no effect on the values of the independent variables. 数组插槽和变量之间没有后续的隐式关系,因此对数组的更改不会影响自变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript传递引用如何解释此行为? - How does JavaScript pass-by-reference explain this behavior? 通过引用传递 JavaScript 对象 - Pass-by-reference JavaScript objects JavaScript 是按引用传递还是按值传递语言? - Is JavaScript a pass-by-reference or pass-by-value language? 使用包装程序在Python / Javascript中模仿按引用传递-良好做法? - Mimicking Pass-By-Reference in Python/Javascript Using Wrappers - Good Practice? 通过javascript中的引用传递和修改变量 - Pass and modify a variable by reference in javascript JavaScript中的按值传递和按引用传递有什么区别? - What's the difference between pass-by-value and pass-by-reference in JavaScript? 是否可以通过引用传递或访问实际对象? - Is it possible to pass-by-reference or access the actual object? JavaScript:创建未知维多维数组函数时的按引用传递问题 - JavaScript : Pass-by-reference issues when creating an unknown dimension multi dimensional array function 如何复制指针事件的行为:JavaScript中没有 - How to replicate the behavior of pointer-events:none in JavaScript 高效的 Javascript 代码:最好在 function 中将变量作为 arguments 传递,还是复制变量? - Efficient Javascript Code: better to pass variables as arguments in a function, or to replicate the variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM