First, 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"

(Confusingly,The result of typeof on a null type is "object", however, the ecmascript standard describes it as a unique type.)

To facilitate the manipulation of primitive type values,ecmascript also provides three special reference types:boolean, number, and string. The standard library provides constructors to encapsulate Boolean values, numbers, and strings as objects.These types are similar to other reference types,They also have special behaviors corresponding to their respective basic packaging types.In fact, whenever a primitive type value is read,An object corresponding to the basic packaging type will be created in the background.This allows us to call methods to manipulate this data.

var s1="some text";
var s2=s1.substring (2);
var s3=new string ("some text");

But unlike the original string,The string object is a real object.

typeof s1;//"string"
typeof s3;//"object"

The variable s1 in this example contains a string,Strings are of course primitive values.The next line calls s1's substring () method and saves the returned result in s2. we know,Primitive values ​​are not objects,So logically,They should not have methods (but they do have methods). Actually, in order for us to achieve this intuitive operation,The background has automatically completed a series of processing.When the second line of code accesses s1, the access process is in a read mode,That is, read the value of this string from memory.When accessing a string in read mode,The background will automatically complete the following processing:

(1) Create an instance of the string type.

(2) Call the specified method on the instance.

(3) Destroy this instance.

It can be represented using the following code:

var s1=new string ("some text");
var s2=s1.substring (2);

After this processing,Basic string values ​​become like objects.Moreover, the above three steps also apply to the boolean and number types corresponding to the boolean and number types.

Second, the 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 using the new operator,The execution flow is kept in memory until it leaves the current scope.And the automatically created basic wrapper type objects,Only exists during the execution period (instantaneous) of this line of code, and then immediately destroyed.This means that we cannot add properties and methods to properties at runtime.

var s1="some text";
alert (s1.color);//undefined

Of course, you can explicitly call boolean, number, and string to create an object of the basic wrapper type,However, this is not recommended.Calling typeof on an instance of a basic wrapper type will return "object", and all objects of the basic wrapper type will be converted to a boolean true. .

var obj=new object ("some text");
alert (obj instanceof string) //true

It is worth noting thatUse new to call the constructor of the basic wrapper type,It is not the same as directly calling a transformation function with the same name.

var value="25";
var number=number (value);//transition function
alert (typeof number) //number
var obj=new number (var);//Constructor
alert (typeof obj) //object

Third, the basic type characteristics

1. The value of the basic type is not changeable:

No method can change the value of a primitive type,For example, a string:

var name="jozo";
name.touppercase ();//prints "jozo"
console.log (name);//prints "jozo"

You will find that the original name has not changed.Instead, a new string is returned after the touppercase () method is called.

Take a look again:

var person="jozo";
person.method=function () {//...};
console.log (person.age);//undefined
console.log (person.method);//undefined

From the code above,We cannot add properties and methods to primitive types,Once again the basic types must not be changed;

2. The comparison of basic types is the comparison of values:

They are equal only if their values ​​are equal.

But you might do this:

var a=1;
var b=true;
console.log (a == b);//true

Aren't they equal?Actually this is knowledge of type conversion and == operator,In other words, when using == to compare two different types of variables, some type conversion is performed.The comparison like above will first convert true to number 1 and compare it with number 1.The result is true. This is when the two values ​​being compared have different types, the == operator performs type conversion,But when the two values ​​are of the same type,Even == is equivalent to ===.

var a="jozo";
var b="jozo";
console.log (a === b);//true

3. Basic types of variables are stored in the stack area (stack area refers to the stack memory in memory)

If there are the following basic types of variables:

var name="jozo";
var city="guangzhou";
var age=22;

Then its storage structure is as follows:

The stack area contains variable identifiers and variable values.

Fourth, reference type characteristics

Reference types are more fun and interesting.

In addition to the above basic types (number, string, boolean, null, undefined) in javascript are reference types,It can also be said to be an object.An object is a collection of properties and methods.In other words, reference types can have properties and methods.Attributes can in turn contain primitive and reference types.Let's take a look at some characteristics of reference types:

1). The value of the reference type is mutable

We can add properties and methods for reference types,You can also delete its properties and methods,Such as:

var person=();//Create a control object-reference type
person.sayname=function () {console.log (person.name);}
person.sayname ();//"jozo"
delete person.name;//Delete the name attribute of the person object
person.sayname ();//undefined

The above code shows that reference types can have properties and methods.And it can be changed dynamically.

2). The value of the reference type is an object stored in the stack memory and the heap memory at the same time

JavaScript is different from other languages.It does not allow direct access to locations in memory,In other words, you cannot directly manipulate the memory space of the object.So what do we do?In fact, it is a reference to the operation object,So the value of reference type is accessed by reference.

To be precise,The storage of reference types requires the completion of the memory's stack area and heap area (heap area refers to the heap memory in memory).Stack memory holds variable identifiers and pointers to the objects in heap memory.It can also be said that the address of the object in the heap memory.

If there are the following objects:

var person1={name:"jozo"};
var person2={name:"xiaom"};
var person3={name:"xiaoq"};

The three objects are stored in memory as follows:

3). Comparison of reference types is comparison of references

var person1="{}";
var person2="{}";
console.log (person1 == person2);//true

In the above comparison of basic types, it was mentioned that when the types of two comparison values ​​are the sameThe equivalent is to use ===, so the output is true. Look again:

var person1=();
var person2=();
console.log (person1 == person2);//false

Maybe you have seen the flaw,The above comparison is two strings,The following compares two objects,Why are long identical objects not equal?

Do n’t forget, reference types are accessed by reference,In other words, whether the addresses in the heap memory of the two objects are the same,That is obvious,person1 and person2 have different addresses in the heap memory:

So these two are completely different objects,So return false;

Five, simple assignment

When assigning a primitive type from one variable to another,A new value is created on the variable,Then copy the value to the location assigned to the new variable:

var a=10;
var b=a;
a ++;
console.log (a);//11
console.log (b);//10

At this time, the value stored in a is 10. When a is used to initialize b, the value stored in b is also 10, but 10 in b is completely independent of that in aThe value is just a copy of the value in a,Thereafter, these two variables can participate in any operation without affecting each other.

In other words, after the basic type is assigned,The two variables are unaffected by each other.

Object references

When assigning a value of a reference type from one variable to another,It also copies the value of the object stored in the variable into the space allocated for the new variable.As mentioned earlier when referring to types,Stored in the variable is the address of the object in heap memory,So, unlike simple assignment,A copy of this value is actually a pointer,This pointer points to an object stored in heap memory.Then after the assignment operation,Both variables hold the same object address,Then these two variables point to the same object.So changing any of these variables,Will affect each other:

var a=();//a holds an instance of an empty object
var b=a;//both a and b point to this empty object
console.log (a.name);//"jozo"
console.log (b.name);//"jozo"
console.log (b.age);//22
console.log (a.age);//22
console.log (a == b);//true

Their relationship is as follows:

Therefore, the assignment of a reference type is actually the assignment of an object to an address pointer stored in the stack area.So two variables point to the same object,Any operation will affect each other.

  • Previous Follow me javascript arguments object
  • Next C # programming method to add rows to DataTable