JavaScript variables, values, and object descriptions
The understanding of variables, values, and objects in Javascript. In order to simplify the understanding, the first half of the shaving is temporarily related to the execution Context. In addition this article is the personal opinion, if has the question or the Non-positive support, Welcome to propose correction and the discussion!
Directory
- Objective
- Variables and values
- Distinguishing variables from values
- Two major types of JS values
- The difference between heap memory and stack memory
- Values vs. objects
- Combine execution context understanding
Preface reference Source
walnuts, background shade, This article references the following sources
- Uncle tom: Variable Object
Variables and values differentiate between variables and values
Like all other programming languages, JavaScript also has variables and concepts worth
var a = 10; A is a variable, 10 is a value (base type) var b = {}; B is a variable, {} is a value (reference Type)
As in the above code, the left side of the = sign is the variable, and the right side of the = is the Value.
Two major types of JS values
The concept of variables and values is mentioned earlier, followed by a brief introduction of the values
The values of JavaScript fall into two main types:
- One is the basic data type (also called the basic type, the original type), only five kinds (undefined,null,number,boolean,string)
The basic type is characterized by the inability to change after the definition, stored in the stack memory (it can be understood as a Constant)
- A class is a complex data type (also called a reference type, an object type), with only one
The characteristic of a reference type is that after the definition, the value can be changed, the stack memory only holds the variable identifier and pointer, and the specific data is stored in the heap memory (so can be changed)
Object.prototype can be understood as the mother of all things, all other types of objects can be understood as it is produced (such as built-in type array,date, etc.), there is a more special function type (its prototype is Function.prototype , and in fact the prototype is based on object.prototype, the details need to refer to the prototype of the series behind the Article)
- For more information: JavaScript data types
For storage of basic types and reference types, you can use the understanding
That is, if the value of the variable 1 becomes 102, the actual in-stack memory of 101 is not changed, but only in the stack memory of a new one, to store 102 of this Constant. Then point the variable 1 to 102.
and the variable 2 because the stack memory is a pointer, the actual execution of the heap in memory data, so the value of the variable 2 can be arbitrarily changed (the data in the heap memory can be changed)
The difference between heap memory and stack memory
What is the difference between the heap memory and the stack memory?
- Stacks are automatically allocated and recycled during their lifetimes. Faster.
- Larger heap Space. Controlled by the User. The life cycle is controlled by reference Counting. The collector to release the final heap space
And the previous basic type and the reference type of heap, stack allocation, can be understood as follows:
- Basic type General data is Small. So with the stack, the speed is fast
- Reference types have a count and heap to control the release mechanism
Values vs. objects
As mentioned above, the relationship between variables, values, and objects is summarized as Follows:
Combine execution context understanding
The variables, values, and objects of JavaScript are explained Above. next, in conjunction with the execution context of JS in-depth Explanation.
Please look forward to ...
JavaScript building blocks (i) variables, values, and objects