JavaScript Brief introduction of reference type _ basics

Source: Internet
Author: User

Brief introduction
1. Type of reference (Reference type)
A reference type is an intrinsic type in JavaScript. It is mainly as a reference, instead of a variable or function, of course, when the need for real value, but also through it to find the real value.

2. Structure of reference types
The value of a reference type is made up of two parts, one is the object of the object that refers to the value of the reference type, and here we call it base and the object name of the object in base. To represent by pseudo code:

Copy Code code as follows:

var Valueofreferencetype = {
Base: <base Object>
PropertyName: <property name>
};

3. Use Scenarios
There are two use stories for reference types:

(1) in processing a marked characters

Identifiers are variable names, function names, function parameter names, and unrecognized property names in global objects.

(2) When processing a property accessor

Copy Code code as follows:

var foo = 10;
function bar () {}

In the intermediate result of the operation, the reference type corresponds

Copy Code code as follows:

var fooreference = {
Base:global,
PropertyName: ' foo '
};

var barreference = {
Base:global,
PropertyName: ' Bar '
};

There's still a need to explain base, where all the objects or functions in JavaScript have objects, and the people who read the previous article know that there is a variable object in each execution context that specifically manages the variables or functions in the context of the execution.

So when processing the marked characters:

In the global context, there is no doubt that base = = Globalvo = = Gloabal

In the execution context of a function, base = = = Vo/ao

But the processing object properties are:

This is more simple, base = = = Owerobject

4. Get the true value of the reference type
At first we said that the reference type is just a surrogate, not that it doesn't save the real value. When a real value is needed, it can be obtained by a series of internal algorithms. This algorithm, we can use the simple pseudocode to describe:

Copy Code code 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 value of the object property, including the attribute analysis inherited from the prototype chain. All through GetValue we can also easily get the true value of the reference type. The following example:

Copy Code code as follows:

GetValue (fooreference); 10
GetValue (barreference); Function Object "Bar"

So when do we need to get the true value of the reference type?

Typically, a reference type needs to be assigned, involved, or invoked to get the true value through the GetValue method. (Note: Objects obtained through GetValue are no longer reference types)

Relation of reference type to this
Reference types are primarily related to this point in the context of a function, and seem quite different at times, all of which lead to a reference type that specifically explains 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 the context of a function, this is provided by the caller and is determined by the way the function is invoked. If the left side of the call bracket () is a value of a reference type, this is set to the base object (base objects) that refers to the type value, and in other cases (any other property that differs from the reference type), this value is null. However, there is no actual case where this value is NULL, because its value is implicitly converted to the global object when the value of this is null. Note: In the 5th edition of ECMAScript, it is not forced to convert to a global variable, but to undefined.

Here's a discussion of three different situations based on the left side of the call bracket:

(1) The value of the reference type is to the left of the call bracket

This does not require excessive analysis, the base object is the this value, and the base can be found. If it is declared under a global variable, it points to the global object.

Copy Code code as follows:

var myObject = {
Foo:function () {
Console.log (this);
}
}

Myobject.foo (); There is no doubt that the base of Foo is MyObject, so this in the Foo method points to MyObject.

(2) The value of the reference type is to the left of the call bracket, but this value is null

Copy Code code as follows:

function MyFunction () {
var foo = function () {
Console.log (this);
}
Foo (); Ao.foo () => Null.foo ()
}

MyFunction (); Output: Window {Top:window, Window:window ...}

When an intrinsic function is invoked, the base of this intrinsic function should be the active object (OA) in the current execution context, but it is treated as Null in the JavaScript interior when OA is base, and JavaScript certainly does not allow this to occur, All sets the base to the global object (this is the source of the design error in the previous this function call pattern). So in this case, this all points to the global object.

(3) The call bracket is not to the left of the reference type value

Copy Code code as follows:

Examples of simple points
(function () {
Console.log (this); Null => Global
})();

A more complicated 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 immediate function, whose function calls the parentheses to the left is an expression, not a reference.

The second example is a lot more complicated, and we're going to analyze it one by one:

Foo.bar (), this is not in doubt, base for Foo,this Point Foo.

(Foo.bar) (), where a parenthesis is used, which acts as a grouping character, that is, it does not force the reference type to execute the GetValue method, and its execution results are identical to the above.

The next three, in parentheses, are assignment operations, or operations, and comma operations, which force the reference type to execute the GetValue method, returning a function object. This way, the function calls the left side of the parentheses is no longer a reference type, all, this is pointing to the global object.

Summarize

I don't really know much about reference types, just to see Uncle Tom's Blog This chapter, in order to explain the function call pattern of this principle and specifically analyzed, this analysis is extremely, I have always thought that the reference type and reference value should have some relationship, did not think, Its Uncle Bolg is only used to assist in understanding this. As for whether they had anything to do with the relationship, if it was a relationship, I would continue to study.

I hope we can communicate more. Thanks to Uncle Tom here, too.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.