Different in “scope” and “context” in this Javascript code

前端 未结 2 1548
暖寄归人
暖寄归人 2020-12-29 13:58

I\'m using this basic event system in my Javascript code and I\'m trying to document it for my coworkers. I\'m not really sure what the difference is in \"scope\" and \"con

2条回答
  •  余生分开走
    2020-12-29 14:18

    Scope pertains to the visibility of the variables, and context refers to the object within which a function is executed.

    Scope:In JavaScript, scope is achieved through the use of functions. When you use the keyword “var” inside of a function, the variable that you are initializing is private, and cannot be seen outside of that function. But, if there are functions inside of this function, then those “inner” functions can “see” that variable; that variable is said to be “in-scope”. Functions can “see” variables that are declared inside of them. They can also “see” any that are declared outside of them, but never those declared inside of functions that are nested in that function. This is scope in JavaScript.

    Context:It refers to the object within which a function is executed. When you use the JavaScript keyword “this”, that word refers to the object that the function is executing in.

提交回复
热议问题