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

前端 未结 10 1419
北荒
北荒 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:35

    In my knowledge, in Javascript, any variable declared outside of a function belongs to the global scope, and is therefore accessible from anywhere in your code.

    Each function has its own scope, and any variable declared within that function is only accessible from that function and any nested functions. Local scope in JavaScript is only created by functions, which is also called function scope.

    Putting a function inside another function could be one possibility where you could achieve reduced scope ( ie nested scope)

提交回复
热议问题