1. Basic types and reference types
There are 5 basic data types:undefined,boolean,number,string,null
typeof NULL;//"Object"typeof undefined;//"undefined"typeof 1;//"number"typeof false //"Boolean"typeof "1" //"string"
(It is confusing that the typeof operation on a null type results in "Object", however, the ECMAScript standard describes it as a unique type.) )
To facilitate operation of the base type value, ECMAScript also provides three special reference types:Boolean, number, and string, which provides constructors to encapsulate Boolean, numeric, and string as objects. These types are similar to other reference types and also have special behaviors corresponding to their respective basic wrapper types. In fact, whenever a primitive type value is read, the background creates an object of the corresponding basic wrapper type, allowing us to invoke some methods to manipulate the data.
var"some text";var s2 = s1.substring(2);varnewString("some text");
But unlike the original string, the string object is a real object.
typeof s1; //"string"typeof s3; //"object"
The variable in this example S1 contains a string, and the string is, of course, the base type value. The next line calls the substring () method of S1 and saves the returned result in S2. We know that primitive type values are not objects, so logically they should not have methods (but they do have methods). In fact, in order for us to achieve this intuitive operation, the background has been automatically completed a series of processing. When the second line of code accesses S1, the access procedure is in a read mode, that is, the value of the string is read from memory. When accessing strings in read mode, the following processes are automatically completed in the background:
(1) Create an instance of type string.
(2) invokes the specified method on the instance.
(3) Destroy this instance.
You can use the following code to indicate:
varnewString("some text");var s2 = s1.substring(2null;
After this processing, the basic string value becomes the same as the object. Furthermore, the above three steps also apply to Boolean and numeric values that correspond to the Boolean and number types.
2. Life cycle
The main difference between a reference type and a basic wrapper type is the life cycle of the object. An instance of a reference type created with the new operator is kept in memory until the execution flow leaves the current scope. Objects that are automatically created by the basic wrapper type exist only in the execution period (instantaneous) of this line of code and are immediately destroyed. This means that we cannot add properties and methods to the property at run time.
"some text";s1.color"red";alert(s1.color//undefined
Of course, you can display objects that call Boolean, number, and string to create a basic wrapper type, but it is not recommended to do so. Calling typeof on an instance of the base wrapper type returns "Object", and all objects of the basic wrapper type are converted to Boolean true:
varnewObject("some text"instanceofString//true
It is important to note that using new to invoke the constructor of the basic wrapper type is not the same as a transformation function that calls the same name directly.
var value="very";varNumber = number (value);//Transformation functionAlerttypeofNumber//numbervarobj =NewNumber (var);//ConstructorsAlerttypeofObj//object
3. Basic type Features
1. The value of the base type is not to become:
No method can change the value of a primitive type, such as a string:
‘jozo‘//‘JOZO‘// 输出 ‘jozo‘
It will be found that the original name has not changed, but instead the toUpperCase () method is called to return a new string.
Let's look at one more:
‘jozo‘22function(){//...};console.log(person.age); // undefinedconsole.log(person.method); // undefined
The above code shows that we cannot add properties and methods to the base type, and once again the basic type cannot become;
2. Comparison of basic types is a comparison of values:
They are equal only when their values are equal.
But you might be like this:
var1;vartrue;console.log(a == b);//true
Aren't they equal? In fact, this is the knowledge of the type conversion and = = operator, that is, when you compare two different types of variables with = =, some type conversions occur. A comparison like the one above will first convert true to the number 1 and then the number 1, and the result is true. This is when the = = operator does the type conversion when comparing the two value types, but even = = = = = = = = = = = = = = = = = = = = = = = = = When the two values are of the same type.
var‘jozo‘;var‘jozo‘;console.log(a === b);//true
3. Basic types of variables are stored in the stack (stack memory in memory)
If there are several basic types of variables:
var‘jozo‘;var‘guangzhou‘;var22;
Then its storage structure is as follows:
The stack area includes the variable's identifier and the value of the variable.
4. Reference type Features
Reference types can be fun and fun.
In addition to the basic type (number,string,boolean,null,undefined) above, JavaScript is a reference type, and it can be said to be an object. Objects are collections of properties and methods. That is, a reference type can have properties and methods, and a property can contain both a base type and a reference type. Take a look at some of the characteristics of reference types:
1. The value of a reference type is variable
We can add properties and methods for reference types, or delete their properties and methods, such as:
varperson = {};//Create a Control object--reference typePerson.name =' Jozo ';p Erson.age = A;p Erson.sayname = function(){ Console.log (person.name);} Person.sayname ();//' Jozo 'DeletePerson.name;//Remove the Name property of the person objectPerson.sayname ();//undefined
The above code shows that reference types can have properties and methods, and can be dynamically changed.
2. The value of a reference type is an object that is stored in both the stack memory and the heap memory
JavaScript differs from other languages in that it does not allow direct access to the in-memory location, which means that the object's memory space cannot be manipulated directly, so what do we do? In fact, it is a reference to an operand, so the value of the reference type is accessed by reference.
To be precise, the storage of reference types requires that the stack and heap area of memory (the heap is the heap memory in memory) is done together, the stack memory holds the variable identifier and the pointer to the object in heap memory, or the address of the object in the heap memory.
If there are several objects:
var person1 = {name:‘jozo‘};var person2 = {name:‘xiaom‘};var person3 = {name:‘xiaoq‘};
The three objects that are stored in memory are as follows:
3. Comparison of reference types is a reference comparison
var‘{}‘;var‘{}‘;console//true
The basic type of comparison mentioned above when the two comparison value of the same type, equivalent to the use of = = =, so the output is true. Look again:
var person1 = {};var person2 = {};console//false
Perhaps you have seen the flaw, the above comparison is two strings, and the following comparison is two objects, why the long identical objects are not equal?
Remember that reference types are accessed by reference, in other words, if the address in the heap memory of the two objects is the same, it is clear that Person1 and Person2 have different addresses in the heap memory:
So these two are completely different objects, so return false;
5. Simple Assignment
When a base type is assigned a value from one variable to another, a new value is created on the variable and then copied to the location assigned to the new variable:
var a = < Span class= "Hljs-number" >10 ; var b = a;a ++ ; console log (a); //one console< Span class= "hljs-built_in". log (b); //
At this point, a holds a value of 10, when you use a to initialize B, the value saved in B is also 10, but B in 10 and a is completely independent, the value is only a copy of the value in a, after which, these two variables can participate in any operation without affecting each other.
That is, after the assignment operation, the base type has two variables that are not affected by one another.
6. Object references
When a value of a reference type is assigned to another variable from one variable, the value of the object stored in the variable is also copied into the space allocated for the new variable. When referring to a reference type, it is mentioned that the object stored in the variable is an address in the heap memory, so, unlike a simple assignment, a copy of this value is actually a pointer to an object stored in the heap memory. Then after the assignment operation, two variables hold the same object address, the two variables point to the same object. Therefore, changing any of these variables will affect each other:
varA={};//A saves an instance of an empty objectvarB=A//A and B all point to this empty objectA.Name= ' Jozo '; console.Log(A.name);//' Jozo 'Console.Log(b.name);//' Jozo 'B.Age= A; console.Log(b.Age);//Console.Log(A.Age);//Console.Log(A==b);//True
Their relationship is as follows:
Therefore, the assignment of a reference type is actually an assignment of the object's value in the stack address pointer, so that two variables point to the same object, and any operation will affect each other.
JavaScript learns--ITEM4 basic types and basic wrapper types (reference types)