JavaScript reference type

Source: Internet
Author: User

  This article mainly introduces the JavaScript reference type, the need for friends can refer to the

Introduction 1. Reference type (Reference type) 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. The value of a reference type of a struct 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. Use Pseudocode to represent:   code as follows: var valueofreferencetype = {base: <base object>, PropertyName: <property name>};     3. Use story Reference type:   (1) The   identifier is the variable name, the function name, the function parameter name, and the unrecognized property name in the global object when processing an identifier.   (2) When processing a property accessor     code is as follows: var foo = 10; function bar () {}     in the middle result of the operation, the reference type corresponds to the   code as follows: var fooreference = {        Base:globa L,         PropertyName: ' foo '    };       var barreference = {        Base:global,         Propertyna Me: ' Bar '    };     It's still necessary to explain base, where all objects or functions in JavaScript have objects, and as anyone who has read my previous article knows, 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 logo characters:   In the global context, there is no doubt that base = = Globalvo = = = Gloabal   In the execution context of the function, base = = Vo/ao   But placeThe object attribute is:   This is simpler, base = = Owerobject   4. Gets 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 simple pseudocode to describe:   code as follows: function GetValue (value) {    if (Type (value)!= Reference) {    R Eturn value;  }     var base = getbase (value);     if (base = = null) {    throw new Referenceerror;  }     return base. [[Get]] (Getpropertyname (value)); The [[get]] method inside       Returns the true value of the object property, including analysis of the attributes inherited from the prototype chain. All through GetValue we can also easily get the true value of the reference type. The following example:   code is as follows: GetValue (fooreference); GetValue (barreference); Function Object "Bar"     So when do we need to get the true value of the reference type?   is typically a reference type that needs to be assigned, involved, or invoked to get the true value through the GetValue method. (Note: An object acquired through GetValue is no longer a reference type)   The relationship reference type to this is mainly related to this point in the function context, and it seems quite different at times, All we do is introduce a reference type to specifically explain the performance of this in the context of the function. The general rules for determining this value in the context of the   function are as follows:   In a function context, 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. But, the value of this is not actually 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.   Below we will discuss according to the different three kinds of conditions on the left side of the call bracket:   (1) The call to the left is the value of the reference type   This does not need to do too much analysis, the base object is the this value, find base. If it is declared under a global variable, it points to the global object.     Code as follows: var myObject = {     foo:function () {            CONSOL E.log (this);      }  } (); There is no doubt that the base of Foo is MyObject, so this in the Foo method points to MyObject.       (2) call parentheses to the left of the reference type value, but this value is null   code as follows: function MyFunction () {     var foo = functi On () {            Console.log (this);      }         &NBS P;foo ();    // () => ()}   MyFunction (); Output: Window {Top:window, Window:window ...}       When an internal function is invoked, the base of this intrinsic function should be the active object (OA) in the current execution context. But in JavaScript, when OA is base, it is null, and JavaScript certainly does not allow this to be null, and all sets base to global (this is the source of design errors in the previous this function invocation pattern). So in this situationcondition, this points to the global object.   (3) The call to the left of the parentheses is not the value of the reference type     copy code code as follows://Simple Point example (function () {  Console.log (this);//null => global} )();  //Complex example var foo = {  Bar:function () {    console.log (this);  }}; (); Reference, OK => foo ( (); Reference, OK => foo   ( = (); Global (False | | (); Global (, (); 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 each one: (), this is no doubt, base for Foo,this Point Foo.   ( (), 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.   Three, in parentheses, which are assignment operations, or operations, and comma operations, they all 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.   Summary   About reference types, in fact, I have been not very familiar with this, just see Uncle Tom's Blog This chapter, in order to explain the function call pattern of this principle and analyzed specifically, this analysis can not be, I have always thought that there should be some relationship between the reference type and the reference value, but it never occurred to me that Uncle Bolg was just used to help understand this. As for whether they had anything to do with the relationship, if it was a relationship, I would continue to study.   Hope that we can pay moreFlow. Thanks to Uncle Tom here, too.  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.