In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
In this issue, Xiaobian will bring you about variables and scope in JavaScript. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.
JavaScript variables are very different from variables in other languages. JavaScript variables are not mandatory, which determines that they are simply names used to hold specific values at specific times. Since there are no rules defining what data type values a variable must hold, the value of a variable and its data type can change over the life of the script.
variables and scope
1. Values for primitive and reference types
There are two common types of values: primitive type values and reference type values. Primitive type values are those simple data segments that are stored in stack memory, i.e., where such values are stored entirely in memory. Reference type values refer to objects stored in heap memory, meaning that what is stored in a variable is actually 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 type value or a reference type value. Basic type values are: Undefined, Null, Boolean, Number, and String. Each of these types takes up a fixed amount of space in memory, their values are stored in stack space, and we access them by value.
If you assign a value of a reference type, you must allocate space in heap memory for that value. Because such values are not fixed in size, they cannot be saved to stack memory. But memory address size is fixed, so memory address can be saved in stack memory. 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 way you define primitive type values and reference type values is almost identical: you create a variable and assign a value to it. However, when this value is saved to a variable, the operations that can be performed on different types of values vary greatly.
var test = new Object(); //Create reference type
test.name ='qf '; //Add an attribute
alert(test.name); //output
A problem arises when you add attributes to values of primitive types.
var test = 'qf'; //Create a primitive type
test.age = 18; //Add attributes to base types
alert(test.age); //undefined
Copy variable values
Basic types and reference types also differ when it comes to variable replication. The primitive 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 use the typeof operator.
var test = 'qf';
alert(typeof (test)); //string
The above is the JavaScript variables and scope shared by Xiaobian. If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to 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: 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.