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

What should be paid attention to in JS variables

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

Share

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

This article introduces what you need to pay attention to in the JS variable, the content is very detailed, interested friends can refer to, hope to be helpful to you. Variables declared using the var keyword are explicitly declared: for example: var abc1 = 'hello'

Js allows you to implicitly declare variables without using var: for example, abc2 = 'world'

So, the question is, is there any difference between these two variables? Http://www.2798888.com/

1. If both abc1 and abc2 are in global scope, print the window object in the browser console: console.log (window)

As shown below:

As you can see in the above figure, the global variables abc1 and abc2 are both properties of the window object and have global scope.

two。 If both abc1 and abc2 are in function scope, print the window object in the browser console?

As shown below:

As you can see in the above figure, abc1 does not appear in the window object in the function scope and is a local variable, while abc2 is still a property of the window object and has a global scope.

3. If abc1 and abc2 are both in the global scope and are properties of the window object, can they be deleted as object properties?

As shown below:

As you can see in the figure above, the variable abc1 declared with var has not been deleted and is still a property of the window object, while the variable abc2 declared without var has been deleted.

4. Why is there such a difference?

This is related to the default object property descriptor!

As shown below:

As you can see in the figure above: in the descriptor of the property abc1 of the window object, configurable: false indicates that the property is not configurable and cannot be deleted.

five

As shown below:

As you can see in the figure above: in the descriptor of the property abc2 of the window object, configurable: true indicates that the property is configurable and deletable.

It is the different default settings in the object property descriptor that lead to the difference whether it can be deleted or not!

In js, variable declaration and function declaration will appear "declaration promotion". The js engine parses and executes code in two stages: 1. Pre-analysis stage; 2. Line by line execution phase.

In the pre-parsing phase, the variable abc1 is declared and the initial value is assigned to undefined;. In the line-by-line execution phase, the variable abc1 is assigned to 'hello';, so there is no error report before the variable declaration, and the printed value is undefined.

On the JS variables need to pay attention to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Development

Wechat

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

12
Report