Javascript simulation object-oriented full solution (1. Type and transfer) _ javascript skills

Source: Internet
Author: User
First, describe the characteristics of JS. Javascript is a weak type of language. It does not exist in terms of template polymorphism, classes, and structures. However, there are no classes. Why should we talk about object-oriented? It does not matter. It can be simulated. In addition, this method has become a recognized method for implementing object-oriented JavaScript.
In addition, if everything in JS is fully developed, there is no private or protected scope for members.
Next, let's start with the question.

I. Type // starting from the foundation, it seems hard

1. Differences of Types
The basic data type is not the same as the object type.
A. The basic type is only a value with no behavior. The object type has its own behavior.
B. The basic type is the value type, which only represents a value. The object type has many complicated things.
C. Pass the value when the basic type is passed, and transfer the address when the object type is passed.
In addition, the text is very special. JS contains two types of text: A Basic Data Type and an object type. For example:
Var str = "The End"; // This is The basic data type, and The transfer method is to pass The value
Var str2 = new string ("The End"); // This is different. With The new identifier that opens up The memory space for The object, The corresponding variable will become The object type, address Transfer
To put it simply: a. a variable that is directly assigned with a literal value, for example, var a = 1; var B = "a"; var c = true ;, all are basic data types (commonly used values, text, and Boolean)
B. Variables assigned with new values, such as var a = new Object (); var B = new string (); are all Object types (JS has many objects, it is a simplified object-oriented language.) Note: The basic data type can also be new, but it is rarely used. Therefore, the above differentiation method does not fully apply to all situations. Please make your own judgment.

2. parameter transfer mode this section mainly distinguishes between the address and value. Take the example as an example:

The Code is as follows:


Function changeVar (varible ){
Varible = 5;
Alert (varible); // Tip 5
}
Var a = 3;
Alert (a); // prompt 3
ChangeVar (a); // The function contains the code alert (a) for changing parameters. // The error code is 3.


According to the above example, although the function has changed the parameter, it does not change the pass-through variable represented by the parameter. This is to pass the value. When changeVar is called, JS re-copies the variable you passed as the parameter. Therefore, the parameter operated in changeVar is actually a copy of the variable you passed, not itself. It is actually the value of the variable, not the variable itself. This is called passing values.

The Code is as follows:


Function changeVar (varible ){
Varible. x = 5;
Alert (varible. x); // Tip 5
}
Var a = new Object;
A. x = 3 alert (a. x); // prompt 3
ChangeVar (a); // This function contains code for changing parameters.
Alert (a. x); // prompt 5


In the preceding example, the Object is used. It is found that after changeVar, the corresponding attribute of the original variable also changes, and the function is internally the variable passed by the operation. The address transfer principle is to pass the memory address of the given variable. The internal change of the function is actually the variable you pass. Because the operations are all in the same memory address.

However, be sure to pay attention to this ""! The JS transfer address is something special! When passing object types, JS also copies an object of the corresponding type, but all attributes and functions of the copy object are the attributes and functions of the original object. It may be that the attribute is transferred but the object is not transferred. This feature can be proved. The Code is as follows:

The Code is as follows:


Function changeVar (varible ){
Varible = new Object ();
Varible. x = 5;
Alert (varible. x); // Tip 5
}
Var a = new Object;
A. x = 3 alert (a. x); // prompt 3
ChangeVar (a); // This function contains code for changing parameters.
Alert (a. x); // prompt 3


When you change the object represented by the parameter, the object represented by the variable you passed is not changed. As mentioned above, you can change the attributes of the original variable representing the object through the attribute operations on the parameter object in the function. This combination proves that JS also copies an object of the corresponding type when passing the object type, but all the attributes and functions of the copy object are the attributes and functions of the original object.
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.