1. Quote
A reference is a pointer to the actual location of the object. See the following example of using citations:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
In this example, both objects point to the same object, and when modifying the property content of one object, it will affect the other.
Let's look at another example. This time we use arrays to explain the reference:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
If arr is redefined, the reference is not the same object, as shown in the following code:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
Next, let’s take a look at a more special example, about the reference of strings.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
As shown in this example, when performing a string operation, the result will always be a new string object, not a modified version of the string.
I don’t know if you have read the <<JavaScript Advanced Programming>> There is a section in the book that talks about passing values and address. To put it bluntly, it is a quote. If you are interested, you can go and have a look.
JavaScript is a language that maintains a series of references to other objects. Through reference, it can bring great flexibility to the program.
2. Function overloading
The characteristic of function overloading is to perform different functions by overloading functions according to different number or types of parameters passed in. It must rely on two things: one is to judge the number of incoming parameters, and the other is to judge the type of incoming parameters.
2-1, judge the number of incoming parameters
Each function in JavaScript has a variable that only acts within the scope of this function, called a parameter, which is a pseudo-array containing all the parameters passed to the function, although with a length property.
Through arguments, we can get this pseudo-array. As shown below:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
arguments is a very useful thing. Looking at a functional function below, it can convert any number of parameters into an array.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
2-2, determine the type of incoming parameters
The first way to judge the type:
To determine the type, another operator in JavaScript is required to use typeof. It is used to express the type of variable content and returns a string. For example, if a variable is a string, then after typeof, it returns ( "string").
Often we use the following judgment:
if( typeof num == "string" ){
num = parseInt( num );//If it is a string, parse the string out of integers
}
if( typeof arr== "string" ){
arr= (",");//If it is a string, it is divided into an array according to commas
}
For example, change the previous makeArr() function to only accept string-type parameters, the code is as follows:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
The final result is 2, because the last 2 parameters are of type number.
The second way to judge:
This method needs to refer to a property that all JavaScript objects carry, constructor, constructor. This property refers to the function that was originally used to construct the object.
if( == String ){
num = parseInt( num );//If it is a string, parse the string out of integers
}
if( == String ){
arr= (",");//If it is a string, it is divided into an array according to commas
}
if( == Array ){
newArr = (",");//If it is an array, then strings are composed based on commas
}
The result after executing the constructor is aObject, and the result after executing typeof is aString. See the comparison of the table below:
A reference is a pointer to the actual location of the object. See the following example of using citations:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
In this example, both objects point to the same object, and when modifying the property content of one object, it will affect the other.
Let's look at another example. This time we use arrays to explain the reference:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
If arr is redefined, the reference is not the same object, as shown in the following code:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
Next, let’s take a look at a more special example, about the reference of strings.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
As shown in this example, when performing a string operation, the result will always be a new string object, not a modified version of the string.
I don’t know if you have read the <<JavaScript Advanced Programming>> There is a section in the book that talks about passing values and address. To put it bluntly, it is a quote. If you are interested, you can go and have a look.
JavaScript is a language that maintains a series of references to other objects. Through reference, it can bring great flexibility to the program.
2. Function overloading
The characteristic of function overloading is to perform different functions by overloading functions according to different number or types of parameters passed in. It must rely on two things: one is to judge the number of incoming parameters, and the other is to judge the type of incoming parameters.
2-1, judge the number of incoming parameters
Each function in JavaScript has a variable that only acts within the scope of this function, called a parameter, which is a pseudo-array containing all the parameters passed to the function, although with a length property.
Through arguments, we can get this pseudo-array. As shown below:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
arguments is a very useful thing. Looking at a functional function below, it can convert any number of parameters into an array.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
2-2, determine the type of incoming parameters
The first way to judge the type:
To determine the type, another operator in JavaScript is required to use typeof. It is used to express the type of variable content and returns a string. For example, if a variable is a string, then after typeof, it returns ( "string").
Often we use the following judgment:
Copy the codeThe code is as follows:
if( typeof num == "string" ){
num = parseInt( num );//If it is a string, parse the string out of integers
}
if( typeof arr== "string" ){
arr= (",");//If it is a string, it is divided into an array according to commas
}
For example, change the previous makeArr() function to only accept string-type parameters, the code is as follows:
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
The final result is 2, because the last 2 parameters are of type number.
The second way to judge:
This method needs to refer to a property that all JavaScript objects carry, constructor, constructor. This property refers to the function that was originally used to construct the object.
Copy the codeThe code is as follows:
if( == String ){
num = parseInt( num );//If it is a string, parse the string out of integers
}
if( == String ){
arr= (",");//If it is a string, it is divided into an array according to commas
}
if( == Array ){
newArr = (",");//If it is an array, then strings are composed based on commas
}
The result after executing the constructor is aObject, and the result after executing typeof is aString. See the comparison of the table below:
variable | typeof variable | variable.constructor |
{a:"b"} | "object" | Object |
["a","b"] | "object" | Array |
function(){} | "function" | Function |
"a" | "string" | String |
66 | "number" | Number |
true | "boolean" | Boolean |
new User() | "object" | User |
By judging the number and type of incoming parameters, function overloading is simple.