JavaScript intermediate notes (2)

Source: Internet
Author: User
Tags sendmsg
An object is a basic unit of JavaScript. This chapter describes the most important parts of JavaScript: References, function overloading, scope, closure, and context. With this knowledge, object-oriented development will become simple.

1. ReferenceA reference is a pointer to the actual position of an object. See the following example for reference:
<Script type = "text/javascript">
/* Example reference */
Var obj = new Object (); // defines an empty Object
Var newObj = obj; // defines a new object reference.
Obj. name = "rain"; // modify attributes of the original object
Alert (newObj. name );
Alert (obj. name = newObj. name); // it can be proved that they are the same object referenced
</Script>

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:

<Script type = "text/javascript">
/* Example reference */
Var arr = new Array ("a", "B", "c"); // defines an Array
Var newArr = arr; // defines a reference to a new array.
Arr. push ("d"); // Add a value
Alert (newArr [3]);
Alert (arr. length = newArr. length); // it can be proved that they are referenced in the same Array object.
</Script>

If arr is redefined, the reference is not the same object, as shown in the following code:

<Script type = "text/javascript">
/* Example reference */
Var arr = new Array ("a", "B", "c"); // defines an Array
Var newArr = arr; // defines a reference to a new array.
Arr = new Array ("e", "f"); // redefine an Array
// NewArr and arr now point to different objects
// Arr points to new Array ("e", "f ");
// NewArr points to new Array ("a", "B", "c ");

Alert (arr! = NewArr );
</Script>

Next, let's look at a special example about string reference.

<Script type = "text/javascript">
/* Example reference */
Var str = "";
Var newStr = str;
Str = str + "B"; // modify the value of str. Note that this will create a new object instead of modifying the original object.
Alert (str! = NewStr );
</Script>

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 OverloadingFunction 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. Each function in JavaScript carries a variable that only works within the range of this function. It is a parameter that contains all the parameters passed to the function. Pseudo Array, Although the length attribute exists. With arguments, we can get this pseudo array. As follows:

<Script type = "text/javascript">
// Define a simple function for sending information
// Obj is the object to be sent, and msg is the message to be sent
Function sendMsg (obj, msg ){
If (arguments. length = 2 ){
Alert (obj + "Send:" + msg );
} Else {
Alert ("the number of parameters is incorrect and cannot be sent ");
}
}

// When only one parameter is used
SendMsg ("hello ");
// When two parameters are used
SendMsg ("instructor Li", "hello ");
</Script>

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 <arguments. length; I ++ ){
Arr. push (arguments [I]); // Add a value
}
Return arr; // returns an array.
}

Var a = makeArr ("a", "B", 3, 4 );
Alert (a. length );
</Script>

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:

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 <arguments. length; I ++ ){
If (typeof arguments [I]! = "String") {continue ;};
Arr. push (arguments [I]); // Add a value
}
Return arr; // returns an array.
}

Var a = makeArr ("a", "B", 3, 4 );
Alert (a. length );
</Script>

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 an attribute that all JavaScript objects have, constructor. This attribute references the function originally used to construct the object.

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.