Results 1 to 6 of 6

Thread: [RESOLVED] Use parameter of function as return?

  1. #1
    Fanatic Member
    Join Date
    Sep 05
    Posts
    521

    Resolved [RESOLVED] Use parameter of function as return?

    How would I do something like this:

    Code:
    var theVariable = "";
    var wasSuccess = false;
    
    function doSomething(theParameter)
    {
    
      theParameter = "I'm the parameter!";
    
      return true;
    
    }
    
    wasSuccess = doSomething(theVariable);
    
    alert(theVariable);
    And it will alert "I'm the parameter!".

    Basically I need to change the variable's value that's specified as a parameter. Not sure if it's possible to do with JavaScript, but I'm sure I've seen it done before.

  2. #2
    Frenzied Member kfcSmitty's Avatar
    Join Date
    May 05
    Location
    Kingston, Ontario
    Posts
    1,789

    Re: Use parameter of function as return?

    You'll want to pass the parameter by reference.

    http://php.net/manual/en/language.references.pass.php

    *edit* Whoops... Thought I was in the PHP section. Take a look at this instead: http://snook.ca/archives/javascript/javascript_pass/
    Last edited by kfcSmitty; May 22nd, 2012 at 08:09 AM.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 02
    Posts
    21,636

    Re: Use parameter of function as return?

    well, if it's displaying "I'm the parameter" in the alert box.... doesn't that answer your question?
    Or are you asking if that will work? If that's the case, then it should have taken you all of 5 minutes to test that out. create an HTML file, add the script, open it and see what happens.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.-I also subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  4. #4
    Moderator
    Join Date
    Jan 05
    Location
    Sydney
    Posts
    13,612

    Re: Use parameter of function as return?

    Call-time pass-by-reference is a headache. JavaScript doesn't support it. Instead, you could pass an object containing a property, and modify the value of that property.

    But your example doesn't make a lot of sense, because the new value is known before the function call, and so the function call is redundant. Show us what you really need to do and we'll come up with a way to do it.

  5. #5
    Fanatic Member
    Join Date
    Sep 05
    Posts
    521

    Re: Use parameter of function as return?

    kfcSmitty got it spot on! That's exactly what I was trying to achieve.

    Basically the reason I'm doing this is I've got a JS function that I use for AJAX. It's all fine when I'm updating the webpage, but when I want to update a JS variable, it's a different story.

    Here's what I mean:

    PHP Code:
    var httpReqs = new Array;

    function 
    getFromServer(queryAndURLidToUpdate)
    {
      var 
    newUpper 0;
      
    httpReqs[httpReqs.length+1]=false;
      
    newUpper=httpReqs.length;
      
      if(
    navigator.appName == "Microsoft Internet Explorer")
      {
        
    httpReqs[newUpper] = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else
      {
        
    httpReqs[newUpper] = new XMLHttpRequest();
      }
      
      
    httpReqs[newUpper].open("GET"queryAndURLtrue);
      
    httpReqs[newUpper].onreadystatechange=function()
      {
        if(
    httpReqs[newUpper].readyState == 4)
        {
          
    document.getElementById(idToUpdate).innerHTML httpReqs[newUpper].responseText;
        }
      }
      
    httpReqs[newUpper].send(null);
      
      return 
    0;
      

    I will be modifying this function so that when called, it updates what ever variable that's passed as a parameter.

  6. #6
    Moderator
    Join Date
    Jan 05
    Location
    Sydney
    Posts
    13,612

    Re: [RESOLVED] Use parameter of function as return?

    Why don't you just pass the event handler in?

    Code:
    function getFromServer(queryAndURL, callback)
    {
      // ...
      httpReqs[newUpper].onreadystatechange = callback;
      // ...
    }

Posting Permissions

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