This article mainly introduces the Javascript reference type. If you need it, please refer to it.
Introduction
1. Reference type)
The reference type is an internal type in javascript. It is mainly used as a reference to replace a variable or function. Of course, when you need a real value, you can find the real value through it.
2. Structure of the reference type
The value of the reference type is composed of two parts. One is the object to which the value of the reference type refers. Here we call it base, the second is the name of the object in base. Use pseudo code:
The Code is as follows:
Var valueOfReferenceType = {
Base:,
PropertyName:
};
3. Use Cases
There are two scenarios for reference types:
(1) when processing a identifier
The identifier is the variable name, function name, function parameter name, and attribute name not recognized in the global object.
(2) when processing an attribute accessor
The Code is as follows:
Var foo = 10;
Function bar (){}
In the intermediate result of the operation, the reference type corresponds
The Code is as follows:
Var fooReference = {
Base: global,
PropertyName: 'foo'
};
Var barReference = {
Base: global,
PropertyName: 'bar'
};
It is necessary to explain the base. In javascript, all objects or functions have objects. Anyone who has read the previous article knows that, each execution context has a variable object dedicated to managing the variables or functions in the execution context.
Therefore, when handling the identifier:
In the global context, there is no doubt that base = globalVO = gloabal
In the context of function execution, base = VO/AO
However, the property of the processing object is:
This is even simpler. base === owerObject
4. Get the true positive value of the reference type
As we mentioned at the beginning, the reference type is just a reference, not a reference that does not save the real value. When you need a real value, you can get it through a series of internal algorithms. This algorithm can be described using simple pseudocode:
The Code is as follows:
Function GetValue (value ){
If (Type (value )! = Reference ){
Return value;
}
Var base = GetBase (value );
If (base = null ){
Throw new ReferenceError;
}
Return base. [[Get] (GetPropertyName (value ));
}
The internal [[Get] method returns the true values of object attributes, including the analysis of inherited attributes in the prototype chain. All real values of the reference type can be easily obtained through GetValue. For example:
The Code is as follows:
GetValue (fooReference); // 10
GetValue (barReference); // function object "bar"
So when do we need to obtain the true positive value of the reference type?
Generally, the reference type requires a value assignment, calculation, or call. The GetValue method is used to obtain the real value. (Note: The object obtained through GetValue is no longer of the reference type)
Relationship between the reference type and this
The reference type is mainly closely related to this in the context of the function, and it looks quite different from each other. All of us will introduce the reference type to specifically explain the performance of this in the context of the function.
The general rules for determining this value in the function context are as follows:
In a function context, this is provided by the caller and determined by the method of calling the function. If the left side of the brackets () is the reference type value, this is set to the base object of the reference type value ), in other cases (any other attribute that is different from the reference type), the value is null. However, the actual value of this is null, because when the value of this is null, its value is implicitly converted to a global object. Note: In ECMAScript 5th, the value is undefined instead of being converted to a global variable.
We will discuss the following three situations based on the differences on the left of the call brackets:
(1) The left side of the call bracket is the reference type value.
This does not require much analysis. The base object is the value of this. You can find the base. If it is declared under a global variable, it points to the global object.
The Code is as follows:
Var myObject = {
Foo: function (){
Console. log (this );
}
}
MyObject. foo (); // undoubtedly, the base of this foo is myObject, so this in the foo method points to myObject.
(2) the left side of the call bracket is the reference type value, but the value is null.
The Code is as follows:
Function myFunction (){
Var foo = function (){
Console. log (this );
}
Foo (); // AO. foo () => null. foo ()
}
MyFunction (); // output: Window {top: Window, window: Window ...}
When an internal function is called, the base of the internal function should be the active object (OA) in the context of the current execution, but the base of the javascript internal OA is treated as null, javascript certainly does not allow the occurrence of this as null, and all of them set the base as a global Object (this is the source of a design error in the previous this function call mode ). In this case, this points to a global object.
(3) the left side of the call bracket is not a reference value.
The Code is as follows:
// Simple example
(Function (){
Console. log (this); // null => global
})();
// Complex example
Var foo = {
Bar: function (){
Console. log (this );
}
};
Foo. bar (); // Reference, OK => foo
(Foo. bar) (); // Reference, OK => foo
(Foo. bar = foo. bar) (); // global
(False | foo. bar) (); // global
(Foo. bar, foo. bar) (); // global
When the left side of the call bracket is not a reference type but another type, this is automatically set to null and the result is a global object.
In the first example, the function immediately calls an expression on the left side of the parentheses, not a reference.
The second example is much more complicated. Let's analyze it one by one:
Foo. bar (). There is no doubt that base is foo, and this points to foo.
(Foo. bar) (), a parentheses are used here. It acts as a grouping character, that is, it does not force the reference type to execute the GetValue method. The execution result is exactly the same as above.
The following three parentheses are values, values, or values, and comma operations. They force the reference type to execute the GetValue method and return a function object. In this way, the reference type is no longer on the left side of the function call parentheses. All of them point to the global object.
Summary
I don't know much about the reference type, but I only saw this chapter in Uncle Tom's blog. In order to explain the principle of this in the function call mode and analyze it specially, this analysis is incredible. I have always thought that there should be some relationship between the reference type and the value passed by the reference. I did not expect that bolg is only used to help understand this. There is no relationship between the two. If there is a relationship between them, I have to continue to study.
I hope you will have more exchanges. Thanks to Uncle Tom.