JavaScript Intermediate Notes chapter II _javascript tips

Source: Internet
Author: User
1, reference
A reference is a pointer to the actual position of the object. Look at the following examples of using references:
<script type= "Text/javascript" >/* Examples Reference * * var obj = new Object (); Defines an empty object var newObj = obj; Defines a new object reference. Obj.name = "Rain";//Modify the property alert (newobj.name) of the original object; Alert (Obj.name = = newobj.name);//This can prove that they are the same object referenced </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

In this case, two objects all point to the same object, and when you modify the property contents of one object, it affects the other.
Let's take another example, this time we'll use an array to explain the reference:
<script type= "Text/javascript" >/* Examples Reference * * var arr = new Array ("A", "B", "C"); Defines an array of var newArr = arr; Defines a reference to a new array. Arr.push ("D");//Add a value of alert (newarr[3]); Alert (Arr.length = = newarr.length);//It can be proved that they are the same array objects referenced </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

If the ARR is redefined, the reference is not the same object, as shown in the following code:
<script type= "Text/javascript" >/* Examples Reference * * var arr = new Array ("A", "B", "C"); Defines an array of var newArr = arr; Defines a reference to a new array. arr = new Array ("E", "F"); Redefine an array//newarr and arr now point to a different object//arr point to the new array ("E", "F"); The newarr points to the new Array ("A", "B", "C"); alert (Arr!=newarr); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Let's take a look at a more specific example of a reference to a string.
<script type= "Text/javascript" >/* Examples quote * * var str = "a"; var newstr = str; str = str + "B"; change the value of STR, note that this will create a new object rather than modifying the original object alert (STR!=NEWSTR); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

As this example shows, when a string operation is performed, the result is always a new string object, not a modified version of the string.
I don't know, you've seen it. <<javascript Advanced Program Design >> a section of the book is about the value and the transfer of the site, that is, the reference. Be interested to go and have a look.
JavaScript is a language that maintains a series of references to other objects by referencing them, which gives the program great flexibility.
2, function overload
The feature of a function overload is to perform different functions through overloaded functions, depending on the number or type of arguments passed in. It must depend on 2 things: one is to judge the number of incoming arguments, and the other is to judge the type of incoming arguments.
2-1, determine the number of incoming parameters
Each function of JavaScript has a variable called a parameter that functions only within the scope of the function, and it is a pseudo array that contains all the arguments passed to the function, although there is a length property.
Through arguments, we can get to this pseudo array. As shown below:
<script type= "Text/javascript" >//define a simple function to send information//obj for the Sent object, MSG for the Sent information function sendmsg (obj, msg) {if (argument s.length==2) {alert (obj + "send:" + msg); }else{alert ("Incorrect number of parameters, cannot be sent"); }///sendmsg ("Hello") only with one parameter; Sendmsg with 2 parameters ("Teacher Li", "Hello"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

Arguments is a very useful thing, looking at the following function, which converts any number of arguments to an array.
<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 Value} return arr;//returns array} var a = Makearr ("A", "B", 3,4); alert (a.length); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

2-2, determine the type of incoming parameter
The first way to judge a type:
The judge type needs to use another operator--typeof in JavaScript. It is used to express the type of the variable's contents and returns a string. For example, if a variable is a string, then the typeof is returned ("string").
Often we use the following judgments:
Copy Code code as follows:

if (typeof num = "string") {
num = parseint (num);//If it is a string, parse the string out of an integer
}
if (typeof arr== "string") {
Arr= arr.split (","); if it is a string, divide the array by commas
}

For example, to change the previous Makearr () function to accept only parameters of string type, 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 value} return arr;//returns array} var a = Makearr ("A", "B", 3,4); alert (a.length); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The final result A.length is 2, because the following 2 parameters are number types.
The second way of judging types:
This method needs to refer to a property with all JavaScript objects, constructor--constructor. This property refers to the function that was originally used to construct the object.
Copy Code code as follows:

if (Num.constructor = = String) {
num = parseint (num);//If it is a string, parse the string out of an integer
}
if (Arr.constructor = = String) {
Arr= arr.split (","); if it is a string, divide the array by commas
}
if (Newarr.constructor = = Array) {
NEWARR = Newarr.join (",");//If an array, the string is composed of commas
}

the result of executing the constructor is an object , and the result of executing typeof is a string . Look at the comparison of the following table:
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 arguments, the function overload is simple.

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.