JavaScript Advanced Programming (3rd Edition) Chapter Fourth reading notes

Source: Internet
Author: User

Chapter fourth variables, scopes, and memory issues

  1. The ECMAScript variable may contain values for two different data types: the base type value and the reference type value. A primitive type refers to a simple piece of data, whereas a reference type value refers to an object that may consist of multiple values. The basic data types are: Undefined, Null, Boolean, number, and string. The value of a reference type is an object that is saved in memory. JavaScript does not allow direct access to the in-memory location, that is, the object's memory space cannot be manipulated directly.
  2. You cannot add a property to a value of a base type, but you can add properties dynamically to a reference type value.
  3. When copying a base type value from one variable to another, two variables can participate in any operation without affecting each other, but when copying a reference type value from one variable to another, two variables will actually refer to the same object, that is, changing one variable, which affects the other.
  4. The parameters of all functions in ECMAScript are passed by value, that is, copying the values outside the function to the parameters inside the function, and copying the values from one variable to another.
  5. When detecting values of reference types, the typeof operator is of little use, and to determine what type of object it is, use the instanceof operator with the following syntax: Result = variable instanceof Constructor the value of all reference types is an instance of object, when a reference type value and an object constructor are detected, the instanceof operator always returns true, using the instanceof operator to detect the value of the base type. False is always returned because the base type is not an object.
  6. The execution environment defines other data that a variable or function has access to, and each execution environment has a variable object associated with it, and in a Web browser, the global execution environment is considered a Window object. Each function has its own execution environment. When code executes in an environment, a scope chain (scope chain) of the variable object is created, and the purpose of the scope chain is to ensure an orderly access to all variables and functions that the execution environment has access to. The front end of the scope, which is always the variable object for the environment in which the code is currently executing. For example, when the environment is a function, the active object initially contains only one variable, the arguments object, which does not exist in the global environment. The variable object of the global execution environment is always the last object in the scope chain.
  7. The internal environment can access all external environments through the scope chain, but the external environment cannot access any variables and functions in the internal environment.
  8. Two ways to extend the scope chain: the catch block and the WITH statement for the Try-catch statement.
  9. JavaScript does not have block-level scopes, but in JavaScript, variable declarations in the IF statement add variables to the current execution environment, and the variable I created by the for statement still exists in the execution environment outside the loop even after the for loop execution ends.
  10. Variables declared with Var are automatically added to the closest environment, within the function, the closest environment is the local environment of the function, and in the With statement, the closest environment is the function environment. If you initialize a variable without using the var declaration, the variable is automatically added to the global environment.
  11. In the process of locating identifiers, identifiers in the parent environment are not used if there is an identifier with the same name in the local environment.
  12. JavaScript has an automatic garbage collection mechanism, which works by identifying variables that are no longer in use at regular intervals, and then releasing the memory they occupy. The first garbage collection is marked (Mark-and-sweep), and when the variable enters the environment, it marks the variable as "entering the environment" and marks it as "out of the environment" when the variable leaves the environment. This approach is supported by a variety of browsers. Another less common garbage collection strategy is called reference counting. The meaning of the reference count is the number of times each value is referenced by the tracking record. When the number of references to this value becomes 0 o'clock, it means that there is no way to access the value so that it can be recycled back into the memory space it occupies.
  13. The amount of available memory allocated to a Web browser is typically less than that allocated to a desktop application, and this is done primarily for security reasons, to prevent the system from crashing when the Web page that is running JavaScript is ready to go into full system memory. Once the data is no longer in use, it is best to release its reference by setting it to null, which is called dereferencing, applicable to the properties of most global variables and global objects. The real effect of dereferencing is to leave the value out of the execution environment so that the garbage collector can retract it the next time it runs.

JavaScript Advanced Programming (3rd Edition) Chapter Fourth reading notes

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.