JavaScript intermediate notes Chapter 2

Source: Internet
Author: User

1. Reference
A reference is a pointer to the actual position of an object. See the following example for reference:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
In this example, both objects point to the same object. When modifying the attribute content of an object, the other object will be affected.
Let's take a look at an example. This time we use arrays to explain references:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
If arr is redefined, the reference is not the same object, as shown in the following code:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Next, let's look at a special example about string reference.

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
As shown in this example, when performing a string operation, the result will always be a New String object instead of a modified version of the string.
I don't know if you have read the <JavaScript advanced programming> section about value transfer and address transfer. To put it bluntly, it is a reference. If you are interested, you can check it out.
JavaScript is a language that maintains a series of references to other objects. By referencing, it can bring great flexibility to the program.
2. Function Overloading
Function Overloading is used to provide different functions based on different numbers or types of input parameters. It depends on two things: one is to determine the number of input parameters, and the other is to determine the type of input parameters.
2-1: determine the number of input parameters
Every function in JavaScript carries a variable that only works within the range of this function. It is a pseudo array containing all parameters passed to the function, although it has the length attribute.
With arguments, we can get this pseudo array. As follows:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Arguments is a very useful thing. Looking at the function below, it can convert any number of parameters into arrays.
<Script type = "text/javascript"> function makeArr () {var arr = []; // defines a temporary array for (var I = 0; I
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
2-2: Determine the input parameter type
Method 1:
To determine the type, you need to use another operator in JavaScript -- typeof. It is used to express the type of variable content and returns a string. For example, if a variable is a string, after typeof, return ("string ").
We often use the following judgment:
Copy codeThe Code is as follows:
If (typeof num = "string "){
Num = parseInt (num); // if it is a string, the string is parsed as an integer.
}
If (typeof arr = "string "){
Arr = arr. split (","); // if it is a string, it is split into arrays by commas.
}

For example, to change the makeArr () function to a parameter of the string type only, the Code is as follows:
<Script type = "text/javascript"> function makeArr () {var arr = []; // defines a temporary array for (var I = 0; I
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
The final result of a. length is 2 because the next two parameters are of the number type.
Method 2:
This method needs to reference a property of all JavaScript objects, constructor. This attribute references the function originally used to construct the object.
Copy codeThe Code is as follows:
If (num. constructor = String ){
Num = parseInt (num); // if it is a string, the string is parsed as an integer.
}
If (arr. constructor = String ){
Arr = arr. split (","); // if it is a string, it is split into arrays by commas.
}
If (newArr. constructor = Array ){
NewArr = newArr. join (","); // if it is an array, a string is composed of commas (,).
}

After the constructor is executed, the result isObjectAnd the result after executing typeof isString. See the following table for comparison:

Variable Typeof variable Variable. constructor
{A: "B "} "Object" Object
["A", "B"] "Object" Array
Function (){} "Function" Function
"" "String" String
66 "Number" Number
True "Boolean" Boolean
New User () "Object" User
By judging the number and type of input parameters, the function overload is simple.

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.