10

I want to use global variable 'x' in the below hook funcion.

var x = 10; //global variable

var oldA = a;    

a = function a(param){

    alert(x);        //showing error: x is undefined 

    return oldA(param);

}

How to resolve the error?

3
  • also you should look around Scoping and Hoisting Commented Jun 28, 2013 at 11:24
  • @user1671639: the function assigned to a cannot be hoisted here, as it's an expression. The only thing that gets hoisted here, are the variable declarations Commented Jun 28, 2013 at 11:27
  • Are you in node environment? In that case, try global.x = 10; & alert(global.x). Actually, polluting global object is not a good idea. But, you might want to try it. Commented Jun 28, 2013 at 11:27

2 Answers 2

15

Your code works fine for me, but you might want to resolve x to the global variable explicitly by using window.x.
When not in a browser environment, or an environment where the global object isn't called window, try:

(window || root || global || GLOBAL || this || self || {x: undefined}).x

The {x:undefined} object literal is just to make sure the expression doesn't throw up errors.
I've listed pretty much all names I know of that are given to the (strictly speaking nameless) global object, just use the ones that might apply to your case.

On the other hand, if the global variable x might be reassigned by the time the function (a) gets called, a closure would be preferable:

a = (function (globalX)
{
    return function a(param)
    {
        console.log(globalX);
        return oldA(param);
    };
}(x || window.x));//pass reference to x, or window.x if x is undefined to the scope

Of course, if you're in strict mode, you need to be careful with implied globals, too.
That's all I can think of that is going wrong with your code, some more details might provide us with a clue of what's actually happening...

Sign up to request clarification or add additional context in comments.

3 Comments

@user1671639: thx. And I wouldn't call your comment a mistake, because the variable declarations are hoisted, after all. With the OP's snippet as is, oldA will be undefined, and oldA(param) will throw an error either way. Providing the OP with a link on hoisting is a nice contribution either way.
@EliasVanOotegem ( root || window ) throws error in mozilla firefox console. ReferenceError: root is not defined
@Mohammed if course, root is an alias for the global object in nodeJS. In browsers, just use window
0

To access global Js variable inside function, don't use Var in function scope and mention var in global scope. Eg.

<script>
    var foo = "hello";
    function fxn() {
        alert(foo);
        foo = "bai";
    }
    fxn();

    alert(foo+"out side");
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.