JS: variables, scopes, and memory issues

Source: Internet
Author: User

Recently reread the "JavaScript Advanced Program Design" This book, "Wen So know New", the Ancients did not bully me, look at the time to find a lot of knowledge is not understood before jumping, there are a lot of just know and remember, and can not form a system, and constantly review, and constantly summarize, I found that there are a lot of fragmented knowledge points in the inadvertently is strung together, the sense of a sudden feeling is very wonderful

And I always like mind map and memory of the way to learn to summarize, in the text expression and summary is very poor, so intends to use this form of blog to record the process of the summary of the stress, when it is a note, also by the way to practice expression ability

Variable:

The variables in JS may contain values for two different data types: a primitive type value and a reference type value. The nature of the distinction depends on how the variable is stored in memory, the base type value is stored in the stack memory, and the reference type value is stored in the heap memory.

Because of the two types of preservation, we will find that when copying the value of a variable, the copy of the value of the underlying type is a value copy, the value of the copied variable is the same as the value of the variable, and is independent in memory; the copy of the reference type value is actually a "value" copy, but this value is a pointer, And the data that we really manipulate is the memory that the pointer is pointing to, and the pointer "value" We copy is just an address to the real data memory, which is what we often call a reference, so in the copy of the reference type, the final copied variable points to the same data as the pre-copied variable. So changing any one of these variables will affect the other

The current simple data types are: Undefined,null,string,number,boolean,symbol (es6 new data type)

Complex data type: Object

For simple data types, the return value is usually judged by typeof: "Undefined", "string", "Number", "boolean", "symbol", "function", "object" and typeof null = = " Object "is due to historical problems, and itself null also means" the variable is an empty pointer ", so the result of its typeof is" object "can also understand

For complex data types, it is common to use instanceof to determine whether it is an instance object of the object that is judged by the prototype chain (which is not discussed).

For some special data types, we also have a corresponding method to determine their type: Data Array.isarray, non-digital isNaN

Execution Environment and scope:

First, clarify a few knowledge points: 1) Each function has its own execution Environment 2) The execution environment defines the other data that the variable or function has access to 3) each execution environment has a variable object associated with it, and all variables and functions defined in the execution environment are stored in this object

For the above three points, I understand that each function has an object associated with it in memory, which holds the variables defined inside the function, methods, and so on, is a descriptive object of the function.

When the function executes, the execution environment of the function is pushed into an environment stack, which forms a "containment" or "parallel" relationship with the other execution environment, and the embodiment of this "inclusion" is the "scope chain", and the more rigorous argument is that when the code executes in another environment, a scope chain of the variable object is created. And the role of the scope chain is very good, we often say that the variable lookup from the local scope to the outer layer to the outer layer until the global scope is dependent on this scope chain. The scope chain can only be looked up.

Memory Issues :

JS's garbage collection mechanism allows the execution environment to manage the memory used during code execution, and according to our understanding of the execution environment and scope, we know that the execution environment is rolled out of the environment stack after the execution of the code, and the local variables in the execution environment do not exist, so At this point you need to tell the execution environment that the local variables should be recycled, and that the specific recycling policy is in the browser:

1: Tag clear: Tags of variables from "go into execution environment" to "leave execution environment", recycle mechanism just clear those variables marked "Leave"

2: Reference count: When a variable is declared and a reference type value is assigned to the variable, the reference number of the value is added 1, and if the same value is assigned to another variable, minus 1, the recycle mechanism is responsible for recovering the value of 0 references. However, when this method encounters a circular reference, it is deadlocked, so the method uses fewer scenarios

The more the confusion of the feeling, the ability to express is too short, but also need more practice, knowledge also to understand deeply, refueling!!!

JS: variables, scopes, and memory issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.