JS Red Book Study Notes-Chapter Four

4.1 Basic type value and reference type value

  • ECMAScript variables can contain two different types of data: primitive values ​​and reference values.
  • The primitive value is the simplest data,
  • A reference value is an object composed of multiple values.
Original value Reference value
Undefined、Null、Boolean、Number、String和Symbol object
The variable that saves the original value is accessed by value, and the operation is the actual value stored in the variable The variable holding the reference value is accessed by reference. The operation is the reference to the object (reference) rather than the actual object itself
The original value cannot have attributes, although trying to add attributes to the original value will not report an error Its properties and methods can be added, modified and deleted at any time
When assigning directly, the original value will be copied to the location of the new variable When assigning a reference value from one variable to another, the value stored in the variable will also be copied to the location of the new variable
  • Strings in JS are not reference types.
  • Symbol (symbol) is new in ECMAScript 6.
  • Object is a collection of unordered key-value pairs.

4.1.1 Dynamic properties

  • The initialization of primitive types can only use primitive literal forms. If the new keyword is used, JavaScript will create an instance of the Object type, but its behavior is similar to the original value.
    let name1 = 'name1';
    let name2 = new String('name2');
    name1.age = 16;
    name2.age = 16;
    console.log(typeof name1,name1.age); // string undefined
    console.log(typeof name2,name2.age); // object 16

4.1.2 Copy value

4.1.3 Passing parameters

  • The parameters of all functions in ECMAScript are passed by value. This means that values ​​outside the function will be copied to the parameters inside the function, just like copying from one variable to another
  • The basic type, the passed parameter is in the function, which is equivalent to the copy of the local variable parameter in the function.
  • The reference type is equivalent to a shallow copy into the function. At this time, the operation to modify the reference type parameter will modify the original value.
// 在函数内部,obj和person都指向同一个对象。
function setName(obj){
    
    
	obj.name = 'test1'
	console.log("obj.name",obj.name) // test1
}
let person = new Object()
setName(person)
console.log("person.name",person.name)  // test1
  • Second example
function setName(obj){
    
    
    obj.name = 'test1'; // 这里我理解为 obj 与 person 指向同一对象,相当于person浅拷贝一份到obje
    obj = new Object(); // 这里 obj 指向了新的对象
    obj.name = "test2222"; // 从这里开始操作新对象
    console.log("obj.name",obj.name); // test2222
    // obj 本地对象在函数执行结束时就被销毁了
}
let person = new Object();
setName(person);
console.log("person.name",person.name) ; // test1

This shows that after the value of the parameter in the function is changed, the original reference remains unchanged. When obj is overwritten inside a function, it becomes a pointer to a local object. And that local object is destroyed at the end of the function execution.

4.1.4 Determine the type

  • Use typeof to determine the type

The typeof operator is best used to determine whether a variable is of a primitive type. More precisely, it is the best way to determine whether a variable is a string, value, boolean or undefined

	let a = 'aaaa';
	let b = true;
	let c = 22;
	let u ;
	let n = null;
	let o = new Object();
	console.log(typeof a); //  string
	console.log(typeof b); //  boolean
	console.log(typeof c); //  number
	console.log(typeof u); //  undefined
	console.log(typeof n); //  object
	console.log(typeof o); //  object
  • Use instanceof to determine the type
  • We usually don’t care if a value is an object, but want to know what type of object it is
  • ECMAScript provides the instanceof operatorresult = variable instanceof constructor
	let obej = new Object();
	let arr = new Array();
	let reg = /59/
	console.log( obej instanceof Object); // true
	console.log( arr instanceof Array);  // true
	console.log( reg instanceof RegExp); // true
  • By definition, all reference values ​​are instances of Object, so any reference value and Object constructor will return true through the instanceof operator.
  • Similarly, if instanceof is used to detect the original value, it will always return false because the original value is not an object.

4.2 Execution context and scope (didn't understand -_-)

  • The context of variables or functions determines what data they can access and their behavior. Each context has an associated variable object, and all variables and functions defined in this context exist on this object. Although the variable object cannot be accessed through code, it is used for background processing of data.

  • The global context is the outermost context. According to the host environment implemented by ECMAScript, the objects representing the global context may be different. In the browser,The global context is the window object we often call

  • The context will be destroyed after all its code is executed, including all variables and functions defined on it (the global context will be destroyed before the application exits, such as closing the web page or exiting the browser).

  • Temporarily understood as something like scope
  • Identifier resolution during code execution is accomplished by searching the identifier name level by level along the scope chain. The search process always starts from the forefront of the scope chain, and then step by step, until the identifier is found.
  • The internal context can access everything in the external context through the scope chain, but the external context cannot access anything in the internal context.
    The connection between contexts is linear and orderly. Each context can go to the upper-level context to search for variables and functions, but no context can go to the next-level context to search
  • Note that function parameters are considered variables in the current context, and therefore follow the same access rules as other variables in the context.

4.2.1 Scope chain enhancement

  • Although there are two main execution contexts: global context and function context (there is a third context inside eval() calls), there are other ways to enhance the scope chain.
  • 1. Catch block of try/catch statement
  • 2, with statement
  • For the with statement, the specified object will be added to the front end of the scope chain;
  • For catch statements, a new variable object is created, which contains the declaration of the error object to be thrown.

4.4 Summary

  • JavaScript variables can hold two types of values: original value and reference value. The primitive value may be one of the following 6 primitive data types: Undefined, Null, Boolean, Number, String, and Symbol.

The original value and the reference value have the following characteristics.
1. The original value has a fixed size, so it is stored in the stack memory.
2. Copying the original value from one variable to another will create a second copy of the value.
3. The reference value is an object, which is stored in the heap memory.
4. The variable containing the reference value actually only contains a pointer to the corresponding object, not the object itself.
5. Copying a reference value from one variable to another will only copy the pointer, so the result is that both variables point to the same object.
6. The typeof operator can determine the original type of the value, while the instanceof operator is used to ensure the reference type of the value.

  • Any variable (whether it contains a primitive value or a reference value) exists in an execution context (also called scope). This context (scope) determines the life cycle of variables and which parts of the code they can access.

The execution context can be summarized as follows.
1. The execution context is divided into global context, function context and block-level context.
2. Each time the code execution flow enters a new context, a scope chain is created for searching variables and functions.
3. The local context of a function or block can not only access variables in its own scope, but can also access any variables in the containing context or even the global context.
4. The global context can only access variables and functions in the global context, and cannot directly access any data in the local context.
5. The execution context of the variable is used to determine when to release the memory.

  • JavaScript is a programming language that uses garbage collection, and developers don't need to worry about memory allocation and collection.

The JavaScript garbage collection program can be summarized as follows.
1. Values ​​out of scope will be automatically marked as recyclable, and then deleted during garbage collection.
2. The mainstream garbage collection algorithm is mark cleaning, that is, mark the currently unused values ​​first, and then come back to reclaim their memory.
3. Reference counting is another garbage collection strategy that needs to record how many times a value has been referenced. JavaScript engines no longer use this algorithm, but some older versions of IE will still be affected by this algorithm because JavaScript accesses non-native JavaScript objects (such as DOM elements).
4. Reference counting problems can arise when there are circular references in the code.
5. Dereferencing variables can not only eliminate circular references, but also help garbage collection. To facilitate memory reclamation, global objects, global object attributes, and circular references should all be dereferenced when they are not needed.

Guess you like

Origin blog.csdn.net/weixin_42480397/article/details/114970108