0

For example this is our piece of code:

function blabla(A,B) {

    // Something

    httpReq.onreadystatechange = function(A,B) {
        if (httpReq.readyState == 4) {
            // console.log(A,B) says 'undefined'
            // How can I use the values of A and B here?
        };
    }
}
Hypn0tizeR
  • 794
  • 2
  • 11
  • 23

4 Answers4

2

Just use A and B. Closure support (pure, basic feature of JavaScript) will take care of it. See How do JavaScript closures work?

In your case,

function blabla(A,B) {

    // Something

    httpReq.onreadystatechange = function(paramA, paramB) {
        if (httpReq.readyState == 4) {
            // console.log(A,B) prints A and B arguments of blabla
            // console.log(paramA, paramB) prints actual parameters
        };
    }
}
Community
  • 1
  • 1
Alex Pakka
  • 9,466
  • 3
  • 45
  • 69
2

You just use them. You problem is shadowing. The inner function arguments are overwriting the outer function ones because they have the same name.

Normally, any local variables are available with no trickyness to any function declared in the same scope. Meaning you just use them, as long as you dont shadow them with new local variables of the same name.

function blabla(a, b) {

    // Something

    httpReq.onreadystatechange = function(c, d) {
        // logs undefined, because no arguments are actually passed in
        // so the variables are undefined.
        console.log(c, d);

        // log arguments passed to blabla() because it picks up the reference
        // the parent scope.
        console.log(a, b);
    }
}

blabla('Hi', 'There'); // should log "Hi", "There"

This just works, so long as you use a unique variable name for the arguments to each function.

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 1
    This is better than my answer. I was not sure if the onreadystatechange would still be able to pick a and b, because the event could be triggered at a later point. Thats why I stored it in a data object. (It wonders me why you bind an event in a method, since I can't imagine this method be re--usable) – Jareish Sep 19 '12 at 20:36
  • http://stackoverflow.com/questions/111102/how-do-javascript-closures-work posted in another answer should answer how all that works. It's a bit scary sometimes, but used properly, ITS AWESOME – Alex Wayne Sep 19 '12 at 21:12
1

You can store A and B temporary in a variable.

var data = {};
function blabla(A,B) {
    data.A = A;
    data.B = B;

    httpReq.onreadystatechange = function() {
        // How can I lead the values of A and B into this?
        console.log(data.A, data.B)
    }
}
Jareish
  • 772
  • 2
  • 9
  • 24
  • 1
    This "works" but is hardly a good idea... By which I mean, this creates a new object `{}` where you don't need one and puts it in a scope that persists longer than the life of the function. – Alex Wayne Sep 19 '12 at 20:19
0

You need to put the onreadystate change function AFTER the send request, and also set the third parameter of the open function to false, to make it synchronous.

Zane Hitchcox
  • 936
  • 1
  • 9
  • 23