In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is the JavaScript scope chain". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Scope
1. What is the scope?
To put it simply, scope (English: scope) is a set of rules for finding variables by name. Scope can be understood as a closed space, which is closed and will not have an impact on the outside. The outer space cannot access the inner space, but the inner space can access the outer space that wraps it.
2. [[Scopes]] attribute
In javascript, each function is an object, and there are some properties that we can access and some of which we cannot freely access. The [[Scopes]] attribute is one of them, which can only be read by the JavaScript engine.
In fact, [[scope]] is what we often call a scope, which stores a collection of contexts for the run-time of the scope.
Here, because func.prototype.constructor and func point to the same function, here we view the [[Scopes]] property by accessing the prototype object of the function func
3. Domain chain
A collection of execution-time context objects stored in [[scope]], which is chained, which we call scope chains. It is through the scope chain that JavaScript looks for variables, and its search method is to query downward along the top of the scope chain (the function in which the object is found will be found in the function scope chain)
4. Graphical principle of finding variables
/ / take the following code as an example to illustrate the principle of JavaScript finding variables through the scope chain * * function a () {function b () {var b = 123;} var a = 123; b ();} var glob = 100
1. When the global function a () is defined, the scope chain is as follows
The [[Scopes]] attribute of the function points to the scope chain object. In this case, the scope chain has only one key-value pair, which points to the global object, which stores what can be accessed globally, that is, the outermost scope, which can be accessed by everyone.
two。 When the global function a () is activated, the scope chain is as follows
At this point, the first thing the scope chain can access is the key-value pair in Activation Object. If not, then access the global object.
3. When function b is defined in function a (), the scope chain of b is as follows
When b is only defined and not called, the scope chain of b is the same as a
4. When function b in function a () is activated, the scope chain is as follows
The scope chain first points to the Activation Object of the function b (), and the search variables are also accessed in the order of the scope chain. If you find it, stop it.
This is the end of the content of "what is the JavaScript domain chain". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.