0

Possible Duplicate:
Pass Variables by Reference in Javascript

I desperately look for a solution. I have a javascript project to finish and the only thing that stays in my way is this little thing. I only need something like and ampersand in c to put next to a function parameter so it be passed by reference and it would change outside of the function.

Now I know there are other ways. But in my case this is the only thing that will help me. This is a program that creates binary search trees and I originally made it in c++ but I need to convert it to javascript cause I will show how the tree is generated while the code executes. This is my project. So to create the binary structure only something like an ampersand would help me.

function nod()
{
    var info;
    var left;
    var right;
}

var rad;
rad = new nod();
rad = null;

function create(rad,x) // create(nod *&rad, int x) in c++
{
    if(rad==null)
    {
        rad = new nod();
        rad.info = x ;
        rad.left = rad.right = null;
    }
    else
    {
        if(x < rad.info)
        {
            create(rad.left,x);
        }
        else
        { 
            create(rad.right,x);
        }
    }
}

function read(rad) // read(nod *&rad) in c++
{
    var input = [
                    0,
                    10,
                    2,
                    1,
                    8,
                    9,
                    4,
                    5,
                    3,
                    6,
                    20,
                    11,
                    30,
                    21,
                    31,
                    22,
                    23,
                ];
    var i;
    for(i=1;i<=16;i++)
    {
        create(rad,input[i]);
    }
}

read(rad);
Community
  • 1
  • 1
Matt
  • 51
  • 1
  • 4
  • You never say what problem you have that passing by address will solve. Please describe your problem more thoroughly – Hunter McMillen Jun 19 '12 at 12:40
  • oh sorry.. my problem is that.. i need that recursive structure because i need to be able to start from rad and get all the nods like rad at the start is 10.. rad.left is 1 rad.left.right is 2 rad.right is 20... and i need to access it that way.. its a must for my project.... i need it to be that way because i'm making a web page that will have the c++ code displayed on the left and on the right i will have the binary tree generated as the code executes.. by clicking on a button it will execute a line of code... and i need it to be almost identical to the c code so have the minimum of problems – Matt Jun 19 '12 at 21:04

2 Answers2

0

In javascript objects are passed by reference. So try passing your values as objects. Hint: Try passing it like this {value:your_value}

ppsreejith
  • 3,318
  • 2
  • 25
  • 27
  • Down vote, strings and other primitive types are NOT passed by reference. As you stated, objects ARE – Dave Lasley Jun 19 '12 at 12:43
  • Ok thanks, added that. I thought they were passed by reference looking at this http://stackoverflow.com/questions/1308624/pass-a-string-by-reference-in-javascript – ppsreejith Jun 19 '12 at 12:44
  • *Nothing* is passed by reference in JavaScript. Read the accepted answer to the duplicate question. – James Allardice Jun 19 '12 at 12:44
  • The confusion here stems from the fact that what's passed is a reference, but if you reassign the variable inside the function (instead of modifying its contents), the reference is no longer there. So, a reference is passed, but it's not returned, so to speak. As long as you don't reassign the variable passed as a parameter, it'll behave as expected in the "pass-by-reference" model. – Camilo Martin Sep 11 '12 at 07:14
0

Not tested, but if you don't desperately need recursion, you can use this:

function create(rad, x) // create(nod *&rad, int x) in c++
{
  var result = new nod();
  result.info = x;
  result.left = result.right = null;

  while (true) {
    if (x < rad.info) {
      if (rad.left) {
        rad = rad.left;
        continue;
      }
      rad.left = result;
      break;
    } else
      if (rad.right) {
        rad = rad.right;
        continue;
      }
      rad.right = result;
      break;
    }
  }
}
Alexander Pavlov
  • 31,598
  • 5
  • 67
  • 93