Call by reference in JavaScript

Hello

I was wondering if it is possible to pass a variable by reference in JavaScript.
I was looking for it in my book, but couldn’t find it.

Here is what I am trying to do:


var param = 'Success';
test(param);
function test(param) {
   param = 'Success';

   if (condition1) {
      param = 'Failure';
      return;
   }

   // return some value
}

alert(param);

I would param to change to ‘Failure’ if the condition is true.
I cannot use the return value, because it is used to return a valid value from the function.
I don’t want to use global variables

thanks

sorry to revive such an ancient thread but i have not long joined and been scowering the forums for some help and this is the closest i have found.

Continueing on, is it possible to have an object (say a player_name_01 object), of which one of its parameters is its current_HP and then this value be altered (and saved!) by methods (say a spell called “zap”), called by another object (say player_name_02)?

I am designing a game, in case you had not guessed! :stuck_out_tongue:

you could do something like this(oo-style):
function test(param) {
this.param = ‘Success’;

if (condition1) {
this.param = ‘Failure’;
return;
}

// return some value
}
var a=new test();
alert(a.param);

thanks
this is interesting
I will give it some thought

> I would param to change to ‘Failure’ if the condition is true.

This is not possible. In javascript everything is passed by value.

> I cannot use the return value, because it is used to return a valid value from the function.

You can return an object:

return { result: 123; status: “Success” }

or (better) declare a class and use method instead of “just function”.

> I don’t want to use global variables

You can also use function “own” variables for this:



function test(n) {
   test.status = 'Success';

   if (n > 10) {
      test.status = 'Failure';
   }
}


test(5);
alert(test.status)

test(25);
alert(test.status)

thanks
This looks like the solution I would use.
clean and to the point

thanks dude. a bit advanced for me so will take some time studying it.

cheers!

Morlaf

The mains idea is that one object (a person) passes a message to another object.
In this case, the message is, “I want to zap you”.
That’s the part that looks like this:


player.gotZappedBy(me);

I added the random bit in only for interest. You could use the roll of a d20 dice, or some other method to determine how much damage occurs when someone is zapped.

Any changes that happen to a person should be made only from within the scope of that person. Don’t reach in from outside to make changes. Instead, ensure that the object knows how to make those changes to itself. That way you help to dramatically lower the number of potential issues that you may have, and it’s easier to maintain.

tere ar at least anoter 5 “web development” forum/blog/elp with a registered user called Morlaf asking similar questions but this is te 1st time i have recieved such an indepth and enlightening responce. thanks…

more to come for sure!
:wink:

Yes it is possible. One object would have a function that sends a message to a different function.

Here’s a quick example where one player can call a gotZappedBy function of another player. The returned amount is how much the second player got zapped by, which the first player can then use to determine what to do next.



function Player(name) {
    this.name = name;
    this.health = 100;
    this.status = function () {
        alert(this.name + ' has ' + this.health + ' health.');
    };
    this.zap = function (player) {
        var me = this;
        var amount = player.gotZappedBy(me);
        if (amount > 3) {
            alert(me.name + ' has just zapped ' + player.name);
        } else {
            alert(player.name + ' successfully blocked an attack by ' + me.name);
        }
    };
    this.gotZappedBy = function (player) {
        var amount = parseInt(Math.random() * 10, 10);
        this.health -= amount;
        return amount;
    };
}