0

So I want to write a function that will change the input parameter. After some google, I learnt that javascript is unlike C++ and I can't pass by reference easily. But change of field of an object parameter will be reflected. So the following is my attempt.

let message_wrap = { str: encrypted_message };
decrypt(message_wrap);
let decrypted_msg = message_wrap.str;

function decrypt(message_to_decrypt_wrapper) {
    let xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState === 4 && this.status === 200) {
            message_to_decrypt_wrapper.str = this.responseText;
            alert(message_to_decrypt_wrapper.str);
        }
    };
    xmlhttp.open("POST", "decrypt.php", true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send("msg=" + message_to_decrypt_wrapper.str);
}

The alert box in fact gives the decrypted message! But the decrypted_msg variable is not. What is happening!!!!

Fluffy Skye
  • 101
  • 4

0 Answers0