Key points of advanced JavaScript programming (1)

1. A complete JavaScript implementation should consist of the following three distinct parts

  •  Core (ECMAScript)
  •  Document Object Model (DOM)
  •  Browser Object Model (BOM)

 

 2. The Undefined type has only one value, the special undefined. When a variable is declared with var but not initialized, the value of the variable is undefined,

For example: var message;

alert(message == undefined); //true

 

3. Null type is the second data type with only one value, this special value is null. From a logical point of view, a null value represents a null object pointer, which is why "object" is returned when the typeof operator detects a null value, as shown in the following example:

var car = null ;

alert(typeof car); // "object"

 

4. Each instance of Object has the following properties and methods.

  •  constructor: holds the function used to create the current object. For the previous example, the constructor (constructor) is Object().
  •  hasOwnProperty(propertyName): Used to check whether the given property exists in the current object instance (rather than in the instance's prototype). Among them, the property name (propertyName) as a parameter must be specified in the form of a string (for example: o.hasOwnProperty("name")).
  •  isPrototypeOf(object): Used to check whether the passed-in object is the prototype of the passed-in object (Prototypes are discussed in Chapter 5).
  •  propertyIsEnumerable(propertyName): Used to check whether a given property can be enumerated using a for-in statement (discussed later in this chapter). As with the hasOwnProperty() method, the property name as a parameter must be specified as a string.
  •  toLocaleString(): Returns a string representation of the object that corresponds to the locale of the execution environment.
  •  toString(): Returns the string representation of the object.
  •  valueOf(): Returns the string, numeric, or boolean representation of the object. Usually the same as the return value of the toString() method.

5. The break and continue statements are used to precisely control the execution of code in a loop. Among them, the break statement will immediately exit the loop, forcing the execution of the statements after the loop to continue. The continue statement also exits the loop immediately, but continues execution from the top of the loop after exiting the loop.

6. ECMAScript variables may contain values ​​of two different data types: primitive values ​​and reference type values. Primitive values ​​refer to simple data segments, while reference values ​​refer to objects that may consist of multiple values.

function setName(obj) {
 obj.name = "Nicholas";
 obj = new Object();
 obj.name = "Greg";
}
var person = new Object();
setName(person);
alert(person.name); //"Nicholas"

The only difference between this example and the previous one is the addition of two lines of code to the setName() function: one line redefines an object for obj, and another line defines a name property for that object with a different value. After person is passed to setName(), its name property is set to "Nicholas". Then, a new object is assigned to the variable obj with its name property set to "Greg". If person is passed by reference, then person is automatically modified to point to a new object whose name property is "Greg". However, when person.name is accessed next, the value displayed is still "Nicholas". This means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when obj is overridden inside a function, this variable refers to a local object. And this local object will be destroyed immediately after the function is executed.

7. To detect whether a variable is a basic data type? (typeof)

var s = "Nicholas";
var b = true;
var i = 22;
var u;
var n = null;
var o = new Object();
alert(typeof s); //string
alert(typeof i); //number
alert(typeof b); //boolean
alert(typeof u); //undefined
alert(typeof n); //object
alert(typeof o); //object 

Check the reference type, what type of object it is (instanceof)

alert(person instanceof Object); // Is the variable person an Object? 
alert(colors instanceof Array); // Is the variable colors an Array? 
alert(pattern instanceof RegExp); // Is the variable pattern RegExp?

 When a function is detected using the typeof operator, the operator returns "function". This operator also returns "function" for specification reasons when using typeof to detect regular expressions in Safari 5 and earlier and Chrome 7 and earlier. ECMA-262 specifies that any object that implements the [[Call]] method internally should return "function" when the typeof operator is applied. Since the regular expressions in the above browsers also implement this method, applying typeof to the regular expression returns "function". In IE and Firefox, applying typeof to a regular expression returns "object".

8. Garbage collection mechanism (it is not recommended to call it yourself, because the browser's recycling mechanism is optimized and practiced -> 4.3.3)

In fact, it is possible to trigger the garbage collection process in some browsers, but we do not recommend that readers do so. In IE, calling the window.CollectGarbage() method immediately performs garbage collection. In Opera 7 and later, calling window.opera.collect() also starts the garbage collection routine.

9. Detailed explanation of stack memory OR heap memory -> https://blog.csdn.net/xdd19910505/article/details/41900693

10. Summary of some problems with variables, scope and memory

  JavaScript variables can be used to hold two types of values: primitive and reference types. The values ​​of primitive types are derived from the following 5 primitive data types: Undefined, Null, Boolean, Number, and String. Primitive type values ​​and reference type values ​​have the following characteristics:

  •  Primitive values ​​occupy a fixed size of space in memory and are therefore stored in stack memory;
  •  Copying a value of a primitive type from one variable to another creates a copy of the value;
  •  The value of the reference type is an object, which is stored in heap memory;
  •  A variable containing a reference type value actually contains not the object itself, but a pointer to the object;
  •  Copy the value of the reference type from one variable to another variable, the copy is actually a pointer, so both variables end up pointing to the same object;
  •  The typeof operator is used to determine which primitive type a value is, and the instanceof operator is used to determine which reference type a value is. All variables (both primitive and reference types) exist in an execution context (also called scope), which determines the lifetime of the variable and which part of the code can access the variable. Here are a few points to summarize about the execution environment:
  •  The execution environment is divided into the global execution environment (also known as the global environment) and the function execution environment;
  •  Every time a new execution environment is entered, a scope chain is created for searching variables and functions;
  •  The local environment of a function not only has access to variables in the function scope, but also has access to its containing (parent) environment, and even the global environment;  The global environment can only access variables and functions defined in the global environment, not directly. access any data in the local environment;
  •  A variable's execution context helps determine when memory should be freed. JavaScript is a programming language with automatic garbage collection mechanism, and developers do not have to care about memory allocation and reclamation. The garbage collection routines in JavaScript can be summarized as follows.
  •  Values ​​that go out of scope will be automatically marked as recyclable and will therefore be deleted during garbage collection.
  •  "Mark and clear" is the current mainstream garbage collection algorithm. The idea of ​​this algorithm is to add a mark to the value that is not currently used, and then reclaim its memory.
  •  Another garbage collection algorithm is "reference counting", which is based on keeping track of the number of times all values ​​are referenced. JavaScript engines no longer use this algorithm; however, it may still cause problems when accessing non-native JavaScript objects (such as DOM elements) in IE.
  •  The “reference counting” algorithm can cause problems when there are circular references in the code.
  •  Dereferencing variables not only helps eliminate circular references, but also benefits garbage collection. To ensure efficient memory reclamation, global objects, global object properties, and circular reference variables that are no longer in use should be dereferenced in a timely manner.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324679931&siteId=291194637