Wechat official account: [Dafront-end Post] follow Dafront-end Post. Questions or suggestions, welcome to leave messages on the public account.

This is the third day of my participation in the August More text Challenge. For details, see:August is more challenging

variable

Raw and reference values

ECMAScript variables can contain two different types of data: raw values and reference values Raw values: the simplest data (Undefined, Null, Boolean, Number, String, Symbol) Reference values: objects held in memory. When you manipulate an object, you are actually manipulating a reference to that object

Object passes to ECMAScript functions are passed by value

function setName(obj) { 
 obj.name = "James"; 
 obj = new Object(); 
 obj.name = "Koby"; 
} 
let person = new Object(); 
setName(person); 
console.log(person.name); // "James"
Copy the code

Here we can see the final output of “James”, if the person was passed by reference, then the person should automatically change the pointer to an object with name “Koby”. Indicates that when the value of an argument in a function changes, the original reference remains unchanged. When obj is overridden inside the function, it becomes a pointer to the local object. That local object is destroyed at the end of the function.

Determine the type

Typeof is best used to determine whether a variable is a string, number, Boolean, or undefined. For object or null, Typeof returns “object”.

console.log(typeof '123'); // string 
console.log(typeof 23); // number 
console.log(typeof true); // boolean 
console.log(typeof undefined); // undefined 
console.log(typeof null); // object 
console.log(typeof new Object()); // object
Copy the code

If we want to determine what type an object is, we need to use the instanceof operator, and the result depends on the prototype chain of the object, right

console.log(person instanceof Object); // Is the variable person an Object? console.log(colors instanceof Array); // Is the colors variable Array? console.log(pattern instanceof RegExp); // Is the variable pattern RegExp?Copy the code

scope

The context of a variable or function determines what data they can access and how they behave. The global context is the outermost context. Each function call has its own context.

When code execution flows into a function, the context of the function is pushed onto a context stack. After the function executes, the context stack pops up the function context, returning control to the previous execution context. The flow of ECMAScript execution is controlled through this context stack.

When executed, the code in the context creates a scope chain of variable objects. This scope chain determines the order in which the code in each level of context accesses variables and functions. The variable object for the context in which the code is executing is always at the forefront of the scope chain. If the context is a function, its activation Object is used as a variable object. The live object originally has only one definition variable: arguments. (This variable is not available in the global context.) The next variable object in the scope chain comes from the containing context, and the next object comes from the next containing context. And so on down to the global context; The variable object of the global context is always the last variable object in the scope chain.

The context is destroyed after all of its code has been executed, including all variables and functions defined on it (the global context is not destroyed until the application exits, such as closing a web page or exiting a browser).

Scope chain enhancement: In both cases, a variable object is added at the front of the scope chain

  • Catch block of a try/catch statement
  • With statement

Pay attention to the following [big front post]
Let’s learn and make progress together

【 Share, like, watch 】 three consecutive, let more people join us ~~