I. Basic types and reference types
- 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.
- When you assign a value to a variable, the JavaScript parser first determines whether it is a basic type or a reference type, the base data type can manipulate the value stored in the variable directly, and the value of the reference data type is the object that is held in memory, and when the object is manipulated, it actually operates on the object's reference rather than the actual object.
Second, the assignment of variables
Third, transfer parameters
- In JavaScript, parameters are passed in accordance with the
值类型来传递
, even if you pass in a reference typefunction setName (obj) { = "Nicholas"; } var New Object (); SetName (person); alert (person.name) ; // "Nicholas"
The return result of the above code looks like the argument is the pass of the reference type because the start Person object has no properties, and after the SetName method is called, the parameter obj is added with the name parameter. Then the person outside the print person.name unexpectedly is a value, which is very obvious the effect of the reference delivery. But do not be fooled by this phenomenon, JavaScript when passing parameters of the reference type, as long as this parameter does not change, then the reference type to deal with, but only the change effect is completely different.
function setName (obj) { = "Nicholas"; New Object (); = "Greg"varnew Object (); SetName (person); alert (person.name) ; // "Nicholas"
The above example shows the difference, if it is really a reference pass, then obj is re-assigned, and the name parameter is added, and the last person will print the name of Greg, but the result is the original value.
IV. implementation Environment and scope
- Execution Environment (Execution context) Defines the other data that a function or variable has access to, determines their behavior, and each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object.
- The global execution environment is the outermost execution environment, in the Web browser, is actually the window object, therefore, all global variables and functions are created as window properties and methods, an execution environment after all the code is executed, the environment is destroyed, The variables and function definitions stored therein are also destroyed (the global execution environment is not destroyed until the application exits and the browser shuts down)
- Each function has its own execution environment , when the program executes to a function, the function of the environment is created, into the current process of the program, and after the completion of the function, the program process will eject and destroy, return control to the previous execution environment
- when the code is executed in an environment, the scope chain of the variable object is created, the fundamental meaning of which is to guarantee the orderly access of variables in the execution environment at different levels, as follows:
var color = "Blue" function ChangeColor () { var anothercolor =" Red " Span style= "color: #000000;" >; function Swapcolors () { var tempcolor = Anothercolor; Anothercolor = color; Color = Tempcolor; // // here You can access anothercolor and color, but you cannot access Tempcolor
Swapcolors (); }
As you can see from the example, the internal environment can access all the external environments on top of it through the scope chain, but the external environment cannot access any variables and functions in the internal environment. These environments are linear and sequential.
- Each environment can search up the scope chain to query for variables and function names, and any environment cannot enter another execution environment by searching the scope chain down
var color = "Blue"function getColor () { return// "Blue"
The color variable inside the GetColor function is obtained from the inside search to the color of the parent's external environment.
- In the search process, if a local variable with the same name appears, the search stops:
var color = "Blue"function getColor () { var color = "Red"; return //"Red"
Scope of JavaScript variables