Previous words
Javascript data types can be divided into two types:primitive types and reference types.Primitive types are also called basic or simple types.JavaScript basic data types include five types:undefined, null, boolean, number, and string. Reference types are also called complex types.Object in JavaScript. Corresponding to this,Their values are also called raw and complex values, respectively
characteristic
Primitive value
To put it simply:the original value is a fixed and simple value,Is a simple data segment stored in the stack,That is,Their values are stored directly where the variable is accessed.
Raw values are the lowest or simplest form of data or information available in JavaScript.Primitive values are called primitive values,Because they are not detailed.That is,Numbers are numbers,Characters are characters,Boolean values are true or false, and null and undefined are null and undefined. The values themselves are simple,Cannot represent a value composed of other values
What types are primitive types?
There are five types of primitive types:undefined, null, boolean, number, string
We can use typeof to determine if one is in the range of a certain type.
typeof operator
Using the typeof operator on a variable or value will return one of the following values:
note:
1.The return value is a string type.
2.Compared with the original type,There is still a null, this is more special,With typeof (null), "object" is returned. We understand null as a placeholder for object.
Complex value
Complex values can consist of many different types of javascript objects.The size of a complex object in memory is unknown.Because complex objects can contain any value,Instead of a specific known value
Storage method
Stack storage
Because the original value occupies a fixed space,Is a simple data segment,In order to facilitate variable query speed,Store it in the stack
Heap storage
Since the size of complex values will change,So it cannot be stored in the stack,Otherwise it will reduce the query speed of variables,So it is stored in the heap, and the value stored in the variable is a pointer,Point to the memory where the object is stored
interview method
Access by value
Original values are stored and manipulated as non-refinable values.Referencing them will transfer their value
var mystring="foo";
var mystringcopy=mystring;
var mystring=null;
console.log (mystring, mystringcopy);//null, "foo"
Referral access
Complex values are stored and manipulated by reference.Rather than the actual value.When creating a variable containing a complex object,Its value is a reference address in memory.When referring to a complex object,Get the value of the object by its reference address in memory using its name (i.e. variable or object property)
var myobject=();
var copyofmyobject=myobject;//There is no copy value,But copied the reference
myobject.foo="bar";//Operate the value in myobject
//Now if output myobject and copyofmyobject, both will output the foo attribute, because they refer to the same object
console.log (myobject, copyofmyobject);//object {foo="bar"}
Comparison method
The original value is compared with the value,Complex values are compared by reference.Complex values are equal only if they refer to the same object (that is, they have the same address).Even two variables that contain the same object are not equal to each other,Because they don't point to the same object
var price1=10;
var price2=10;
var price3=new number ("10");
var price4=price3;
console.log (price1 == price2);//true
console.log (price1 == price3);//false
price4=10;
console.log (price4 == price3);//true
console.log (price4 === price3);//false
var objectfoo={same:"same"};
var objectbar={same:"same"};
console.log (objectfoo == objectbar);//false
var objecta={foo:"bar"};
var objectb=objecta;
console.log (objecta == objectb);//true
Dynamic attribute
For complex values,You can add properties and methods to it,You can also change and delete its properties and methods;But simple values cannot add properties and methods
Complex values support dynamic object properties,Because we can define objects,Then create a reference,Update the object again,And all variables pointing to the object are updated.A new variable points to an existing complex object,The object was not copied.This is why complex values are sometimes referred to as reference values.Complex values can have as many references as needed,Even if the object changes,They also always point to the same object
var str="test";
str.property=true;
console.log (str.property);//undefined
var obja={property:"value"};
var pointer1=obja;
var pointer2=pointer1;
obja.property=null;
console.log (obja.property, pointer1.property, pointer2.property);//null null null
type of packaging
When the original value is used as an object created by the constructor,javascript will convert it into an object,So that the properties and methods of the object can be used,Then discard the nature of the object,And change it back to the original value
Related articles
- Analysis of JavaScript primitive values and object references
- JavaScript position and size (1) correctly understand and use size-related DOM attributes
- angularjs custom ng-model tag attributes
- JS Object-oriented (3) Object class, static properties, closures, private properties, the use of call and apply, three implement
- AngularJS directive returns object properties
- Two ways to dynamically change the action value attribute in the form with JS
- JavaScript detects primitive values, reference values, attributes
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- python - you may need to restart the kernel to use updated packages error
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- python 3x - typeerror: 'method' object is not subscriptable
- javascript - how to check if an element exists in puppeteer
- xcode - pod install [!] no `podfile 'found in the project directory
- i want to call a child component method from a parent in vuejs
- vuejs - [vuetify] unable to locate target [data-app] i want to unit test to avoid warning