Javascript simulated object-oriented full solution (II. Type and assignment) _ javascript skills

Source: Internet
Author: User
Last time, I talked about the differences between types and transfer. Now, I want to add some questions about type assignment. Type assignment is similar to variable transfer.
You do not need to specify the basic data type.

The Code is as follows:


Var a = 5;
Var B =;
B = 3;
Alert (a); // prompt 5
Alert (B); // prompt 3


From this perspective, it is found that changing B will not change a, because the values of all variables are passed back and forth, and there is no relationship with the variable itself.
Object type.

The Code is as follows:


Var a = new Object ();
A. x = 5;
Var B =;
B. x = 3;
Alert (a. x); // prompt 3
Alert (B. x); // prompt 3


As a result, B is a, a is B, and change each other.
If not, let's take another example:

The Code is as follows:


Var a = new Object ();
A. x = 5;
Var B =;
Alert (B. x); // prompt 5, B. x is a. x, both are 5
A. x = 3; // change a. x to B. x, which is 3.
Alert (a. x); // prompt 3
Alert (B. x); // prompt 3


However, the assignment of object type attributes is irrelevant to the object type and has a relationship with the attribute type.
If the attribute is of the basic data type, the value is transmitted. If the attribute is of the object type, the data is transferred. For example:

The Code is as follows:


Var a = new Object ();
A. x = 5; var B = new Object ();
B. x = a. x; B. x = 3;
Alert (a. x); // prompt 5
Alert (B. x); // prompt 3


The attributes of two different objects are isolated from each other and are basic data types. Therefore, values are transmitted to each other without affecting each other.

The Code is as follows:


Var a = new Object ();
A. x = 5;
Var B = a. x;
B = 3;
Alert (a. x); // prompt/5
Alert (B); // prompt 3


This is also the case. The basic data type of B and the attribute x of the basic data type of a are assigned values to each other and will not affect each other, but only pass values to each other.
However, if the object property is also of the object type, it will also be the address.

The Code is as follows:


Var a = new Object ();
A. x = new Object;
A. x. n = 5;
Var B = a. x;
Alert (B .n); // prompt 5
B. n = 3; // The n of a. x is changed.
Alert (a. x. n); // prompt 3
Alert (B .n); // Tip 3


The property x of Object a is defined as the Object type. Therefore, when B is assigned a. x, they are connected. In fact, they are the same and can affect and change each other.
----
However, if I want to assign a value to an object type variable, I just copy the attribute, instead of reaching the "you are me, I am you" realm and the "same Life and Death" noble character. What should I do?
Wood has any special good way. Use the following function.

The Code is as follows:


Var DeepCopy = function (destination, source)
{
For (var property in source)
{
Var copy = source [property];
If (destination = copy) continue;
If (typeof copy = "object ")
{
Destination [property] = DeepCopy (destination [property] | |{}, copy );
}
Else
{
Destination [property] = copy;
}
}
Return destination;
}


Usage

The Code is as follows:


Var a = new Object;
A. x = 5;
A. y = 3;
Var B = new Object;
DeepCopy (B, );
Alert (B. x); // prompt 5
Alert (B. y); // Tip 3
B. x = 8;
Alert (a. x); // prompt 5


Check that the modification to B. x does not affect a. x, right?
This DeepCopy is a good function.
The above describes how to simulate "pass value" for object types"
So how can we simulate the "Address Transfer" of the basic data type "?
Is to use an Array object.

The Code is as follows:


Function change ()
{
A [0] = 5;
Alert (a); // prompt 5
}
Var x = [3];
Alert (x); // prompt 3
Change (x );
Alert (x); // prompt 5


Assign a value to [xx], which is actually an Array object with data. Here we use an array containing an element to simulate the address transfer. Because the array is of the object type, it is passed as the address.
Of course, you can also simulate attributes of any object type.
Related Article

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.