Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the problem of javascript prototype chain and scope

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you questions about how to understand the javascript prototype chain and scope. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

As we all know, unlike other OOP languages, js is a weakly typed language with no obvious difference between data types. Another difference is its scope, which is mainly due to the fact that js inherits through prototype chains.

A) js has no block-level scope, the so-called block-level scope is the part of the if,for,while statement. The test is as follows (Chrome browser).

The following is the output

From the results, we can see that there is no block-level scope in js, and it can still output str1.

B) there are only function scope and global scope in js, which is analyzed below.

The following is the output

From the results, we can see that there is a functional scope in js, so str2 is not defined outside fun1. In order to understand the scope of js, it is necessary to involve the prototype chain, which is briefly analyzed below.

C) when the specified object or variable cannot be found in the current scope, js will look up through the prototype chain, that is, to find the parent scope of the current scope, and if it has not been found, it will still look up until it is found, or the root node has not been found.

The following is the output:

From the results, we can see that str4 can still be output in fun2 because its parent scope is global scope and there is a str4 variable in the global scope.

D) with regard to the parent scope mentioned above, there is another case, that is, if the function is called, that is, the reference of the parent scope when the function is nested.

This is easy to confuse, mistakenly thinking that Eric will be output, but the result is not like this. The result is as follows:

From the result, we can see that the output is gaohui rather than Eric, and the parent scope in this case refers to the parent scope of the local scope where the function is defined, not the parent scope of the place where the function is called.

The above is the question of how to understand the javascript prototype chain and scope shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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: 303

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report