SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:
    Code:
    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

  2. #2
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    919
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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);

  3. #3
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    this is interesting
    I will give it some thought

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > 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:

    Code:
    function test(n) {
       test.status = 'Success';
    
       if (n > 10) {
          test.status = 'Failure';
       }
    }
    
    
    test(5);
    alert(test.status)
    
    test(25);
    alert(test.status)

  5. #5
    SitePoint Addict jasongr's Avatar
    Join Date
    Jul 2004
    Location
    usa
    Posts
    371
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks
    This looks like the solution I would use.
    clean and to the point

  6. #6
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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! :-P

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Morlaf View Post
    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! :-P
    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.

    Code javascript:
     
    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;
        };
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  8. #8
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks dude. a bit advanced for me so will take some time studying it.

    cheers!

    Morlaf

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,712
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    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:

    Code javascript:
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Member
    Join Date
    Oct 2010
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!
    ;-)


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •