Object Name-Reference

Source: Internet
Author: User

Introduction:This is a simple article.ArticleIn fact, it is a reference problem, but it is quite difficult.

Problem:

VaR F = Function (){ This . A = 1}
F. Prototype. B = 33
VaR A1 = New F ()
F. Prototype = {
B: 3
}
Console. Log (a1. B )//?

 

Analysis:

Here the result is 33 or 3. It seems to be 3 at the beginning. It seems that 3 covers the original 33.
If you analyze the process of object generation. F itself is an object A, new F. Generates a new object C.
This object has a _ PROTO _ property link to F. prototye object B. Now C has pointed to B.
Then, F. prototype, that is, B, points to the D object {B: 3 }.

So here we have three objects: A, B, C, and D. View

 

 

 

Summary:

Now F. prototype is originally directed to B object, and now it points to D object.
So do not regard F. prototype as an object. It points to an object.
The object itself has no name. The new object itself is a built-in, identical object. The new process is just a link.
Only various attributes and references are added.

The value of the basic type has a fixed size in the memory, so it is stored in the stack. When you copy the basic type value, a copy is created.
The value of the reference type is an object and is saved in the heap. When copying a reference type value, the pointer of the object is copied. Therefore, both variables point to an object.

 

Extension
Result: a1. B = 33.
If you want to get 3, you can reference the original function through the constructor, and then output the current B. A1. _ PROTO _. constructor. Prototype. B = 3
Here, A. Prototype references the B object, and now it is changed to reference the D object. If object B does not reference object C, it is automatically cleared by the garbage collector.

 

Small questions

The following is the topic referenced in the authoritative guide.

 

VaR A = [1, 2, 3];
VaR B =;
A [0] = 99;
Alert (B );

 

Do not consider a as [, 3], as a points to the array [, 3]
B =. B also points to the array [1, 2, 3]
This makes it clear.

Question 2
Here AA. Prototype. B === B. _ PROTO _. B?

FunctionAA (){
This. A = 2;
AA =Function(){
This. A = 3;
}
AA. Prototype. B =Function(X) {alert (x )};
}
VaRB =NewAA ();
Alert (AA. Prototype. B === B. _ PROTO _. B );
Console. Log (AA. prototype, B. _ PROTO __);

The result shows AA. prototyop! = B. _ PROTO _ Why?
Because B. _ PROTO _ points to the original AA. prototype. So when we look at the constructor, we can't just see the function itself, but there is actually an implicit prototype attribute.

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.