JavaScript intermediate notes Chapter 2 _ javascript skills

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. Reference
A 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; // define a new object reference. Obj. name = "rain"; // modify the attribute alert (newObj. name); alert (obj. name = newObj. name); // it can be proved that they are the same object referenced by script.
[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:

<Script type = "text/javascript">/* example reference */var arr = new Array ("a", "B", "c "); // define an array var newArr = arr; // define 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
[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:

<Script type = "text/javascript">/* example reference */var arr = new Array ("a", "B", "c "); // define an array var newArr = arr; // define 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
[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.

<Script type = "text/javascript">/* example reference */var str = "a"; var newStr = str; str = str + "B "; // modify the str value. Note that this will create a new object instead of modifying the original object alert (str! = NewStr); script
[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 what you 've seen < > Here is a 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:

<Script type = "text/javascript"> // defines 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, cannot be sent ");}} // if only one parameter is used, sendMsg ("hello"); // If two parameters are used, sendMsg ("instructor Li", "hello"); script
[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:

The 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.

The 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 is ObjectAnd the result after executing typeof is String. 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.