JavaScript Learning Notes (iii)

Source: Internet
Author: User

What to learn in this chapter:

1. Use of arrays

2. Class and object details.

Use of 3.this Keywords

4. Constructors, use of member functions

1. Use of arrays

In any language, it is necessary to have an array, with an array, so that a lot of operations have become very convenient. JS inside is also contains a

arrays, JS arrays and Java arrays are basically the same in usage, but they have some more features .... Let's look at a piece of code ...

//First method of Creationvar array=new Array (); //var array=new Array (2); Array[0]=1; array[1]= "AB" //array[3]=" SS "; Span style= "color: #0000ff;" >for (var i=0;i<arr.length;i++// second way to create var array1= ("Java", "C + +", "Python" for (var i=0;i<arr.length;i++              

The above involves only the creation of arrays: There are two ways to create an array, in fact the comment section above is also the way to create an array:

The difference is that the comment section gives the initial length of the array as 3, but we know that JS is a dynamic language, even if we specify an array

The initial length, when we store more data than its length, JS will still dynamically open up new address space, not like Java,

Once you specify the length of the array, it is not allowed to cross ...

common methods for related arrays

1.concat (Array1.conact (ARRAY2,ARRAY3)) merges two or more arrays:

2.length () Take the length of the array

3.toString () Converts a string to an array and returns the result

//First method of Creationvar array=NewArray ();//var array=new array (2); Array[0]=1; array[1]= "AB"; array[2]= "AC"; Document.writeln (array.tostring ());  Convert to String var array1=new Array ("Java", "C + +", "Python"); Document.writeln (Array.concat (array1));  merges two arrays of document.write (array.length); For (var i=0;i<array.length;i++)// fetch length {document.write (array[i]);} </script >         

The above uses three methods, here is just a few simple introduction of a few common methods, followed by the other methods of the array ... We can

See, after merging two arrays, the length of the two arrays does not increase, not the size of the merged array will change ....

2. Class and object details:

JS also has the class and the object ...

<script language= "javascript" type= "Text/javascript" > Person=NewObject ();//create a new object and add properties to the objectPerson.firstname= "Bill"; Person.lastname= "Gates"; Person.age=56; Person.eyecolor= "Blue"; //The second way of building ... Define a constructor     functionPerson1 (firstname,lastname,age,eyecolor) {varName= "SS";//Private:          This. firstname=firstname;//Public-          This. lastname=LastName;  This. age=Age ;  This. eyecolor=Eyecolor;  This. show=function()//Public Functions{Window.alert ("..."); }} per=NewPerson1 ("Tom", "John", "Yellow"); Document.writeln (Per.firstname+ "" +per.lastname+ "" +per.age+ "" +Per.eyecolor); </script>

It is very simple to implement the creation of the object ... After the object has been created, we can add properties directly to the object. You can also create objects

Call the class method: We can see that in JS there are only public and private permissions, and the public member or function uses this off

The key word to implement.

add a bit of knowledge. Object reference

<script language= "javascript" type= "Text/javascript" >functionPerson () {}varA=NewPerson (); A.name= "Xiao Ming"; A.age= "10"; varb=A; B.name= "Small white";
The number of references to the object at this time is 2 Window.alert (A.age+" "+a.name);//Small white window.alert (b.age+" "+b.name);//Small white ten B=NULL;//The reference number of the object is 1 window.alert (A.age+" "+a.name);//Small white window.alert (b.age+" "+b.name);//undefined undefined
The a=null;//object has a reference count of 0 (the GC starts the recycle mechanism)</script>

The above refers to the object reference ... The reference to an object involves a GC (object recycling mechanism). Both A and B point to the same address.

This address is in a heap area where all the properties of this object are saved. The GC determines whether to reclaim objects based on the number of times the object is called.

3.this keywords

to avoid assigning variables to objects every time they are created ... So use the This keyword ...

 <script language= "javascript" type= "Text/javascript" > function  Span style= "color: #000000;"         > person () { this . Name= "abc" ; 
var name= "SSS"; this . age= "; var p1=new person (); var p2=new person (); P2.name = "ss"; // Changing the value of P2 does not affect P1 document.write (p1.name+ "+ </script>

This implements the initialization of the object every time the object is created ... And the use of the This keyword makes the members in person programming public. We

can be accessed externally ... In fact, you can also access the private members outside. We only need to define a public function of this within the person.

Then we call the function internally and we can access the private members.

In a nutshell: Which object instance calls this function, then this represents which object.

<script language= "javascript" type= "Text/javascript" >      function Test ()//  The function defaults to the Window object       {          alert (this. v);      }       var v=90;      Test (); // Output</script>

The above code output is 90. Then the call to test () <==>window.test () means that the Window object is called: THIS.V on behalf of Window.v<==>var v.

Note: This can only be used in a class. Cannot be used externally ...

4. Constructors and member functions

<! DOCTYPE Html>/*************************constructor**********************/    varperson =function(name) { This. Name =name;        }; varperson =function(name, age) { This. Name =name;  This. Age =Age ;        }; /*************************prototype************************/Person.prototype.getName=function(){//member Function:getname ();        return  This. Name;        }; Person.prototype.getAge=function(){//member Function:getage ();        return  This. Age;        }; /********************test**********************************/        varPerson1 =NewPerson ("John");//Use constructor 1 @param: Name        varPerson2 =NewPerson ("Peter", 2);//Use constructor 2 @param: Name, agedocument.write ("Person1=>name:" + person1.getname () + ", Person1=>age:" + person1.getage () + "<br>"); document.write ("Person2=>name:" + person2.getname () + ", Person2=>age:" + person2.getage () + "<br>"); </script></body>part of the form of prototype can also be,/*************************prototype************************/Person.prototype=NewObject ({getName:function(){            return  This. Name; }, Getage:function(){            return  This. Age; }    });

JavaScript Learning Notes (iii)

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.