Search code examples
javascriptfunctionnullreturn

Javascript object is null after return


So I've been struggling with this and so I came here for answers. I feel like a bit of a JavaScript newbie with this question but here it goes.

I have the following this code:

function a() {
    var ret = {};
    //random stuff that get some other object
    ret = someOtherObject;
    alert(ret);
    return ret;
}

var c = a();
alert(c);

The problem is that "c" is null but "ret" is the correct object. Is the object not cloned on return? Does "ret" get lost and disappears outside the function? I don't get it.

Thanks for your help. :)


Solution

  • I suppose that if the a() showed do not return the right answer there should be another a() which is called instead.

    So the problem is that there is two a() in the code one with the right definition the second you don't show us.