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 and master variables and scopes in JavaScript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to understand and master variables and scopes in JavaScript". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand and master variables and scopes in JavaScript".

Variables in JavaScript are very different from variables in other languages. The JavaScript variable is not mandatory, which determines that it is just a name used to hold a particular value at a particular time. Because there are no rules that define what data type values a variable must hold, the value of a variable and its data type can be changed during the life cycle of the script.

Variable and scope

Values of base types and reference types

Values of two common different data types: basic type values and reference type values. Basic type values refer to simple segments of data that are stored in stack memory, that is, a location where the value is completely stored in memory. Reference type values refer to objects that are stored in heap memory, meaning that what is actually stored in a variable is just a pointer to another location in memory where the object is stored.

When assigning a value to a variable, the parser must determine whether the value is a primitive value or a reference type value. There are several basic type values: Undefined, Null, Boolean, Number, and String. These types occupy a fixed amount of space in memory, and their values are stored in the stack space, which we access by value.

If you assign a value of a reference type, you must allocate space for that value in heap memory. Because these values are not fixed in size, they cannot be saved to stack memory. But the memory address size is fixed, so the memory address can be saved in stack memory. In this way, when querying variables of reference type, the memory address is read from the stack, and then the value in the heap is found by the address.

The basic type value is defined in almost the same way as the reference type value: create a variable and assign a value to it. However, when this value is saved in a variable, the actions that can be performed on different types of values are quite different.

Var test = new Object (); / / create a reference type

Test.name = 'qf'; / / add an attribute

Alert (test.name); / / output

When a property is added to the value of the basic type, a problem occurs.

Var test = 'qf'; / / create a basic type

Test.age = 18; / / add attributes to the base type

Alert (test.age); / / undefined

Copy variable valu

In terms of variable replication, basic types and reference types are also different. The base type copies the value itself, while the reference type copies the address.

Var test = 'qf'

Var test2 = test

Detection type

To detect the type of a variable, we can determine it by the typeof operator.

Var test = 'qf'

Alert (typeof (test)); / / string

Thank you for your reading, the above is the content of "how to understand and master variables and scopes in JavaScript". After the study of this article, I believe you have a deeper understanding of how to understand and master variables and scopes in JavaScript. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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