JS learning notes -- 9 --- variable-scope-memory-related

Source: Internet
Author: User
Tags garbage cleaner

The biggest difference between variables in JS and other languages is that JS is a loose language, which determines that it only stores the name of a specific value at a specific time. Because you do not need to display certain types of values when defining variables, the values and Data Types of variables can be changed randomly in the lifecycle of the script page, other languages are not supported. 1. Basic type and reference type value basic type: refers to the simple data stored in the stack memory, that is, the value is completely stored in a location in the memory. The space they occupy is fixed. Types: Number, Null, Boolean, Undefined, and String [the String type is considered to be of the reference type in other languages]. However, if the new method is used to declare variables, these basic types are also reference types. Reference Type: refers to the objects stored in the heap memory. The real data of these types is stored in the heap memory, while the data stored in the stack memory is only a pointer, this pointer points to an address of this object in the heap memory. [Object Type] replication of the basic type: the size of the space occupied by the basic type in the memory is fixed. during replication, a new space is opened in the stack memory, is accessed by value. Copy of the reference type: because the size of this object is not fixed, it is stored in the heap memory, but the memory address size is fixed, therefore, the stack stores the address of the object in the heap memory. In this way, when looking for a reference, the address is first retrieved from the stack memory, and then the corresponding value is found in the heap memory. This is the reference access. During replication, the value of the stack memory is copied, that is, a copy reference. The two variables point to the same object in the heap memory. 2. You can directly add the Dynamic Property Reference Type Variable, box. name = 'abc'; if an attribute is added in the same way as a variable of the value type (literally declared), no error is returned. However, when an attribute is output in the same way, it is undefined, however, if you declare it in the new way, the effect is the same as that of the Object. 3. Copy the basic type of the variable value: copy the value of the variable itself, after replication, a space is re-opened in the stack space. After replication, the two variables are independent of each other and are irrelevant. Therefore, the value of one variable does not affect the value of another variable after it is changed. Reference Type: during replication, only the address of the stack space is copied, And the heap memory is the same object, that is, the reference of replication, the result is that the two variables point to the same memory block. Therefore, if one of the two variables changes the object value after copying, the output result of the other variables also changes, because they point to the same object in the memory. 4. When passing parameters in JS, all parameters are passed through values, and there is no reference transfer. In C language, the reference transfer is to add an & symbol before the variable. Value Type: the value of the variable itself is passed, which is the same as the value of the copied variable. Changing the value of the variable in the function does not affect the reference type of the source variable value. It is also a value transfer, the address value in the memory space of the variable stack is passed. If the value of an attribute of the object is changed in the function, the value in the source variable will also change, because in the heap memory they are pointing to the same object 123456789 function func (num) {num. name = 123 ;}var box ={}; box. name = 'abc'; alert (box. name); // abcd func (box); alert (box. name); // The value 123 is changed in the function func. The parameters in the ECMAScript function are local variables and are not passed according to references. Only variables of the reference type are passed. 5. Detection types are known in JS learning notes-3 --- data types. typeof can be used to detect the types of variables, however, this is only a few basic data types returned. It is not so useful when detecting the Object type, because Null, Object, Array, RegExp, and so on will return the object, in this way, you do not know the type of the variable. Instanceof can be used to determine whether a variable is a specific reference type. Alert (box instanceof Array); alert (box instanceof Null); alert (box instanceof RegExp); alert (box instanceof String); true is returned if it is of this type, otherwise, false is returned. instanceof is used to detect that a variable of the basic type declared in the literal form returns false, however, variables declared using the new method will return true. 6. The execution environment and the scope execution environment, that is, the scope is a very important concept in many programming languages, specifies the permissions for variables or functions to access other data and their respective actions. The execution environment in JS is similar to that in other languages. The global execution environment is the peripheral execution environment. on the Web, the global execution environment is considered as a window object. Therefore, all variables and functions are created based on the properties and methods of the window object. After the code in the execution environment is executed, the execution environment will be destroyed, and the variables and functions in the environment will be destroyed. The global execution environment is destroyed only after the webpage is closed or the program is executed. The function body also contains functions. Only this function can access the functions at the first layer. When a function is called, an environment object is created. When a function is executed, the environment of the function is pushed to the Environment stack for execution. After the environment stack is executed, the stack pops up again, and the control is handed over to the upper-level execution environment. When code is executed in an environment, a block-level function Chain Domain is formed to ensure orderly access to functions and objects with access permissions in the execution environment, the variable object in the execution environment is the frontend of the Chain Domain. Generally, an object is only accessible to its own attributes and methods. It cannot be accessed by multiple generations. window cannot access the ziFunc method defined in func, however, ziFunc, refer to the C # point access method. 7. Block-level scope does not exist. Block-level scope indicates block blocks with curly braces, such as if. Therefore, variables can be defined by condition judgment. Variables defined in code blocks such as if and for can be accessed outside curly brackets, this is very different from other languages. 8. Differences between the var keyword in the function when declaring a variable in the function, if the keyword var is not added, the variable will be considered global, you can also access the function outside. Of course, you need to execute the callback function before accessing the function. if you add the callback function, it is local. It is best not to initialize the variable without using the var keyword, in this case, various errors may occur. When initializing all variables, you must add the keyword var. Generally, when determining a variable, you can search for it. Now, you can find the scope at this level, if no, search for it in the upper-level scope, and so on. Therefore, accessing local variables is more efficient than accessing global variables. Because there is no need to trace the scope chain up to 9, memory-related JS also has a garbage collection mechanism, similar to C #, we do not need to worry about memory leakage issues, the garbage collection mechanism automatically manages memory allocation and useless memory collection. The most common method of garbage collection in JS is to clearly mark the variables in the memory during runtime, and then remove the variables being used in the environment, variables that are not marked out are considered as the variables to be deleted. Finally, the garbage cleaner completes the memory cleanup, destroys the marked variables, and recycles the memory space they occupy. The Garbage Collector runs periodically instead of at any time, this may cause some performance problems, but generally you do not need to worry about this problem. In general, to ensure that the page occupies less memory, you can get better performance for the page, the best way to reduce memory usage is to assign a null value to a variable or object when it is no longer used, that is, box = null; to release the reference. This method is called deleting the reference, this method uses most global variables and global objects.

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.