In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what are the JavaScript basic data types and reference data types". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what the JavaScript basic data types and reference data types are.
ECMAScript contains values of two different data types: basic type values and reference type values. Basic type values refer to simple data segments, while reference type values refer to objects that may be made up of multiple values.
When assigning a variable, the parser must determine whether the value is a primitive type value or a reference type value. The five basic data types commonly used are Undefined, Null, Boolean, Number and String. The basic data type is accessed by value because you can manipulate the actual value stored in the variable. The value of the reference type is the object stored in memory. JavaScript does not allow direct access to locations in memory, that is, you cannot directly manipulate the memory space of objects. When you manipulate an object, you are actually manipulating a reference to the object rather than the actual object. Therefore, the value of the reference type is accessed by reference.
1. Dynamic attribute
For the value of a reference type, we can add properties and methods to it, or we can change and delete their properties and methods. For example:
Var person = new Object (); person.name = "John"; alert (person.name); / / "John"
This property exists as long as the object is not destroyed or the property is not deleted. However, we cannot add attributes to the values of the base type, although it does not cause any errors.
two。 Copy variable valu
If you copy a value of the base type from one variable to another, a new value is created on the variable object and then copied to the location assigned to the new variable, for example:
Var num1 = 5; var num2 = num1; / / 5
The following figure vividly shows the process of copying basic type values:
When you copy a value of a reference type from one variable to another, a copy of the value stored in the variable object is also copied into the space allocated for the new variable. The difference is that a copy of this value is actually a pointer to an object stored in the heap. At the end of the copy operation, the two variables actually refer to the same object. Therefore, changing one of the variables affects the other, such as:
Var obj1 = new Object (); var obj2 = obj1; obj1.name = "John"; alert (obj2.name); / / "John"
3. Transfer parameters
The arguments of all functions in ECMAScript are passed by value. Copying a value outside a function to a parameter inside a function is like copying the value of one variable to another. Whether you pass a value of a base type or a value of a reference type. It should be noted here that there are two ways to access variables by value and by reference, but parameters can only be passed by value.
When you pass a value of the base type to a parameter, the passed value is copied to a local variable (that is, a named parameter). For example:
Function addTen (num) {num + = 10; return num;} var count = 20; var result = addTen (count); alert (count); / / 20, unchanged alert (result); / / 30
Parameters are actually local variables of the function. The parameter num and the variable count do not know each other, they just have the same value. If num is passed by reference, the value of count will also become 30, reflecting changes within the function.
When you pass a value of a reference type to a parameter, the address of the value in memory is copied to a local variable, so the change in this local variable is reflected outside the function. For example:
Function setName (obj) {obj.name = "John";} var person = new Object (); setName (person); alert (person.name); / / "John"
Inside this function, obj and person refer to the same object. In other words, even if the value is passed by value, obj accesses the same object by reference. Therefore, adding the nama attribute to obj in the content of the function also responds to the person object outside the function, because the object that person points to is only one in heap memory and is a global object. Many developers believe that objects modified in the content of the function will be reflected in the global scope, indicating that the parameters are passed by reference. This view is actually wrong. See the following code:
Function setName (obj) {obj.name = "John"; obj = new Object (); obj.name = "Evan";} var person = new Object (); setName (person); alert (person.name); / / "John"
If person is passed by reference, then person is automatically modified to point to the new object whose name property is "Evan", but when we visit person.name, we find that its value is still "John", which means that even if the value of the parameter is changed inside the function, the original reference remains the same. In fact, when you override obj inside a function, this variable refers to a local object. The local object is destroyed immediately after the function is executed.
4. Detection type
To detect whether a value is a basic data type, the typeof operator is a * * tool. It can detect whether a variable is a string, numeric value, Boolean value, or undefined. If a variable is an object or null, the typeof operator returns object.
But typeof is not so useful when detecting the value of a reference type. Usually, we don't want to know that a value is an object, but we want to know what type of object it is, we can use the instanceof operator. If the variable is an instance of a given reference type, the instanceof operator returns true. For example:
Alert (person instanceof Object); / / is the variable person Object? Alert (colors instanceof Array); / / is the variable colors Array? Alert (pattern instanceof RegExp); / / is the variable pattern RegExp?
The values of all reference types are instances of Object. Therefore, when you detect a value of a reference type or an Object constructor, instanceof returns true. When a value of a primitive type is detected with the instanceof operator, false is returned because the primitive type is not an object.
At this point, I believe you have a deeper understanding of "what JavaScript basic data types and reference data types are". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.