- Basic types and reference types
There are two types of data type values in JavaScript. The base type value and the reference type value. A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may consist of multiple values. There are 5 basic data types in JavaScript, namely: Undefined, Null, Boolean, number, String (this is not the same as other programming languages, you need to be aware ). Basic data types are accessed by value and are generally stored in the stack. The value of the reference type is stored on the memory heap, and a reference to it is saved on the memory stack (similar to the way C#,java stores objects in programming languages such as the. Provides basic reference type data in JavaScript, which are: Object, Array, function, and so on. These are detailed in the following blog post.
Let's look at the differences between the basic data types and the reference data types at some key points. Look at the basic data type first.
For example: Var num1=5;var num2=num1. The basic data type is in memory.
As a reference type, we can use the following code to see its memory layout. The code is as follows:
1 var New Object (); 2 var obj2 = obj1; 3 obj1.name = "Hello"; 4 alert (obj2.name); // Output Hello
All function parameters in JavaScript are passed by value. This is a good understanding of the basic data type, which is to copy a current value. Parameters used inside the function and externally passed values do not affect each other. As a parameter of a reference type, this value is actually a reference to the object's memory on the heap. This is similar to the section above that describes the memory layout of reference types.
- Execution Environment and scope
The execution environment defines the other data that variables or functions have access to, and determines their behavior. Each environment variable has a specific variable object associated with it. All variables and functions defined in the execution environment are stored in this variable.
The global execution environment is the outermost execution environment, which is typically referred to as a Window object under the Web environment. All global variables and functions are created as properties and methods of the Window object. Once the code for an execution environment is executed, the environment is destroyed (to be exact, ejected from the environment stack), and all the variables and functions stored in it are destroyed. The global execution environment needs to wait for the browser to close before exiting.
Each function has its own execution environment. When code executes in an environment, the scope chain of the variable object is created. The purpose of a 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 chain, which is always a variable object for the current execution environment. The next variable object in the scope chain contains the external environment, and the next variable object is from the next containment environment. This continues to the global execution environment. The following is an example of the visual look at this aspect of the content.
Let's take a look at this simple piece of code first:
1 function A (x, y) {2 var b=x+y; 3 return b; 4 }
When function A is created, its scope chain fills in the global variable object, which contains global variables and functions. :
If the execution environment is a function, then its active object (Activation object, AO) acts as the first object of the scope chain, the second object is the containing environment, and the next is the containment environment that contains the environment (this is the scope chain we talked about). Or look at the code:
1 function A (x, y) {2 var b=x+y; 3 return b; 4 }5var tatal=a (5,10);
When executing to the 5th line of code and entering function A for execution, the scope chain
The type of execution environment in JavaScript can be divided into two types, global and local (functions). However, some statements can temporarily add a variable object to the front of the scope chain during execution, and the variable object is removed after the code executes. Specifically, a variable object is added temporarily when the execution stream enters the catch block in Try-catch and with the WITH statement. But this can also cause performance loss when building large-scale.
- JavaScript parameter-related knowledge points
The parameters in JavaScript functions are different from other programming languages. The JavaScript function does not mind how many arguments you pass, even if the arguments and parameters do not correspond, because JavaScript uses a arguments object in the function to hold all parameters that are passed externally. The arguments object is an array-like object that can provide arguments[0],[1] in such a way as to access the passed arguments. but it's not an array, it's an object .
1 function Showargs (arg1, arg2) { 2 var len1 = showargs.length; // output parameter number 3 var len2 = Arguments.length;// output argument number 4 alert (LEN1); // output 2 5 alert (LEN2); // output 4 6 7 Showargs (1, 2, 3, 4);
We can see that all parameters passed by the argument are stored in the arguments object, which gets the arguments from the next bidding clubs. The length of the function object only gets the number of the function parameters. The following describes another heavyweight attribute of argument callee. For callee, it is a pointer to its function (the function that the arguments object belongs to). Let's take a look at this property by example.
1 varsum =functionsum (n) {2 varFunctioncode =arguments.callee.toString ();3 //alert (functioncode);4 if(N <= 1) {5 return1;6 }7 Else {8 returnn + arguments.callee (n-1);9 }Ten }; OneAlert (sum (100));//output 5050
This example is calculated 1+2+3+.....+99+100. We can use Arguments.callee to make recursive calls. Arguments.callee points to a pointer to the current function. The comment out of that sentence will output the source code of the SUM function. Well, today's blog is written here. The following are also about objects and closures that will be introduced in subsequent blogs.
variables, parameters, scopes, and scope chains in JavaScript