Is it possible to restrict the scope of a javascript function?

前端 未结 10 1424
北荒
北荒 2020-12-16 10:50

Suppose I have a variables in the global scope.

Suppose I wish to define a function which I can guarantee will not have access to this variable, is there a

10条回答
  •  北海茫月
    2020-12-16 11:41

    Create a local variable with the same name. If you have a global variable like this:

    var globalvar;
    

    In your function:

    function noGlobal(); {
        var globalvar;
    }
    

    If the function refers to globalvar, it will refers to the local one.

提交回复
热议问题