JavaScript (1)

Source: Internet
Author: User

Just like the question!

1. Variables
There are two ways to define
1) the first value assigned to a variable is defined as: Name = "name ";
2) use the keyword VaR to define variables such as VAR name = "name" (recommended)
Naming rules:
1) The first letter must be a letter, underscore, or dollar sign.
2) Other letters can be underscores, dollar signs, or numbers.
2. Original Type
There are five types: undefined, null, Boolean, number, and string. (You can use the typeof operator to obtain the type of a variable)
For example:

Code:
  1. VaR svalue = "string ";
  2. Alert (typeof svalue); // output string

3. type conversion

4. Classes and objects
Like the Java syntax, the new keyword is used to create an object. For example, create an array object:

Code:
  1. VaR array1 = new array ();
  2. Array1 [0] = "V1 ";
  3. Array1 [1] = "V2 ";


If class construction has no parameters, you can also omit the brackets.

Code:
  1. VaR array2 = new array;

1) create an object factory function to create a student object. The Code is as follows:

Code:
  1. Function createstudent (ID, name, sex ){
  2. // Create an object
  3. VaR Ostu = new object;
  4. // Define attributes
  5. Ostu. ID = ID;
  6. Ostu. Name = Name;
  7. Ostu. Sex = sex;
  8. // Define the Method
  9. Ostu. Display = function (){
  10. Alert (this. Name + "begin to study ");
  11. }
  12. // Returns an object.
  13. Return Ostu;
  14. }
  15. // ===== Call ========
  16. VaR ostu1 = createstudent ("34", "Yang long", 18 );
  17. Ostu1.display ();

2) constructor (similar to the factory method)

Code:
  1. Function display () {// similar to a global function
  2. Alert (this. Name + "begin to study JS ");
  3. }
  4. Function Stu (ID, name, sex ){
  5. This. ID = ID;
  6. This. Name = Name;
  7. This. Sex = sex;
  8. This. Display = display;
  9. }
  10. VaR stu2 = new Stu ("19", "nothing", 19 );
  11. Stu2.display ();

3) Prototype Method

Code:
  1. // Create an empty Constructor
  2. Function Stu (){
  3. }
  4. // Use prototype to add attributes for Stu
  5. Stu. Prototype. ID = "44 ";
  6. Stu. Prototype. Name = "Biao ";
  7. Stu. Prototype. Sex = "girl ";
  8. Stu. Prototype. Display = function (){
  9. Alert = (this. Name + "begin to study JS ");
  10. }
  11. // The advantage of using the prototype is that you can add new members to an existing class. For example
  12. Stu. Prototype. lenb = function (){
  13. Return this. Replace (/[^/x0-/XF]/g, "##"). length;
  14. }

===*********** Advanced javascript technology *********** =
1. DOM technology Overview
Dom is a technology used to conveniently process hierarchical documents (such as XML and HTML. Dom also provides an API that allows developers to process these documents in an object-oriented manner. For XML documents, there is an xml dom dedicated to processing XML documents, but the DOM mentioned below is worth the HTML Dom.
The main function of Dom is to obtain various elements (such as Div and form) in the HTML tutorial language, so that the information of these elements can be easily obtained, or dynamically add new elements to these elements.
In fact, JavaScript is also required to operate DOM objects. That is to say, JavaScript code is also required to call Dom APIs.
In JavaScript, the object describing Dom is document. In fact, document is not only HTML Dom, but also xml dom.
To directly operate HTML documents, you can use the documentelement attribute. The Code is as follows:
VaR ohtml#document.doc umentelement; // obtain the HTML Object

Instance:

Code:
  1. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
  2. <HTML>
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  5. <Title> JavaScript test01 </title>
  6. <! -- Start to write JavaScript code (after title) -->
  7. <SCRIPT type = "text/JavaScript">
  8. Function showhead (){
  9. VaR ohtml#document.doc umentelement; // obtain the HTML Object
  10. VaR ohead = ohtml. firstchild; // get the head object
  11. Alert (ohead. outerhtml );
  12. Alert (ohead. innerhtml );
  13. }
  14. // Display the body TAG content
  15. Function showbody (){
  16. VaR ohtml#document.doc umentelement; // obtain the HTML Object
  17. VaR obody = ohtml. childnodes [1]; // get the head object
  18. Alert (obody. outerhtml );
  19. Alert (obody. innerhtml );
  20. }
  21. </SCRIPT>
  22. </Head>
  23. <Body>
  24. <Input type = "button" onclick = "showhead ()" value = "show head label"/>
  25. <Input type = "button" onclick = "showbody ()" value = "show Body Tag"/>
  26. </Body>
  27. </Html>


2. Three methods for obtaining HTML elements
There are three methods in Dom to obtain any HTML element in the current HTML document, which is actually the three methods of HTML document:
Getelementbyid: the property value of ID is unique.
The property value of getelementbyname: Name is not unique. For (VAR I = 0; I <otests. length; I ++) Traversal
Getelementbytagname: obtains the maximum range of HTML elements.

Example: Automatic Image Switching

Code:
  1. <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
  2. <HTML>
  3. <Head>
  4. <Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
  5. <Title> JavaScript getdom 2 </title>
  6. <SCRIPT type = "text/JavaScript">
  7. Setinterval ("LoadImage ()", 1000); // start the timer in 3000 milliseconds
  8. VaR images{'01.jpg ', '02.jpg', '03.jpg', '04.jpg', '05.jpg']; // specify the image file name
  9. VaR I = 0; // global variable I is displayed from the first image file
  10. Function LoadImage (){
  11. I ++;
  12. If (I = 5) I = 0;
  13. VaR oimage = Document. getelementbyid ('image'); // obtain the IMG tag
  14. VaR olabel = Document. getelementbyid ('info'); // obtain the label
  15. Oimage. src = './images/' + Images [I]; // assign values to attributes of the IMG tag
  16. Olabel. innertext = images [I]; // display the current image file name and insert the first image name.
  17. }
  18. </SCRIPT>
  19. </Head>
  20. <Body>
  21. Current image name: <label id = "info"> </label> <p> </P>
  22. <SCRIPT type = "text/JavaScript">
  23. VaR olabel = Document. getelementbyid ('info ');
  24. // Display the first image file during initialization
  25. Olabel. innertext = images [I];
  26. </SCRIPT>
  27. </Body>
  28. </Html>

-- Java Web development overview

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.