JavaScript Notes _ Basic syntax

Source: Internet
Author: User

Like other programming languages, JavaScript also has variables, statements, functions, arrays and other common language elements.

1. Variables:

The variable is defined by the keyword var in javascript.

The variable type in JavaScript is a weak type (weak types are both not specified for specific data types)

Cases:

    1. var x = 3;
    2. x = "Hello";

Note: A special constant value in javascript: Undefined, when a variable is used without initialization, the value of the variable is undinfed (undefined).

2. Statement (same format as Java statement)

1) Judgment structure (if statement)

Cases:

    1. if (x==4)//can perform comparison operations
    2. if (x=0)//Can be assigned to the operation, and can also be sentenced, no error.

2) Select structure (switch statement)

Same as Java

3) Loop structure (while statement, do: While statement, for statement)

Example: (Print 99 multiplication table)

  1. var x = "99 multiplication table";
  2. alert (x);
  3. document.write ("<table>");
  4. for (var i=1; i<=9; i++)
  5. {
  6. document.write ("<tr>");
  7. For (var j=1; j<=i; J + +)
  8. {
  9. document.write ("<th>");
  10. document.write (j + "*" + i + "=" + i*j);
  11. document.write ("</th>")
  12. }
  13. document.write ("</tr>");
  14. }
  15. document.write ("</table>");

Note: The difference is that there is no specific data type restrictions, use should be noted.

3. Functions (General format)

1) General format:

For example:

    1. Function Show ()
    2. {
    3. Alert ("show");
    4. alert (arguments.length);
    5. }

Note: JavaScript functions, with no overloaded form of parameters. The parameters are uniformly placed inside the function's inner array arguments.

2) dynamic function definition (functions):

For example:

[JavaScript]View Plaincopy
    1. var show = new Function ("x","y","return x+y;");
    2. alert (show);

3) anonymous functions (typically used to define how events are handled):

For example:

[JavaScript]View Plaincopy
    1. var show = function ()
    2. {
    3. Alert ("function run");
    4. }
    5. Show ();

4) Arguments array

JavaScript syntax, function, you can not explicitly declare the number and type of functions (not recommended), there is a default arguments array, can be dynamic acquisition of real parameters.

For example:

  1. function Myadd ()
  2. {
  3. var sum = 0;
  4. For (var x=0; x<arguments.length; x + +)
  5. {
  6. document.write ("Argu" + x + ":" + arguments[x] + "<br/>");
  7. Sum + = arguments[x];
  8. }
  9. return sum;
  10. }
  11. document.write ("total:" + myadd (1,2,3,4) + "<br/>");

4. Arrays

JS, the array is equivalent to the collection in Java, the length is variable, there is no out of bounds.

Cases:

    1. var arr = [4,1,6,8]; //directly define arrays
    2. var arr = new Array (); //define an empty array
    3. for (var i=0; i<arr.length; i++)
    4. {
    5. Alert (Arr[i]);
    6. }

5, the definition of the object

  1. The definition here is a bit like a constructor.
  2. function person (name,age)
  3. {
  4. this.name = name;
  5. this.age = age;
  6. }
  7. var p = New Person ("Mingzi","1");
  8. P.getname = function () //tool method
  9. {
  10. return p.name;  //or return p["name"];
  11. }

6. With statement

    1. 6) withstatement
    2. With (Stu) //stu is an object
    3. {
    4. Alert (name+"..." +age);
    5. }


7. For-in statement

1) for-in traverse the properties of the class

    1. For (S-in Stu) //s is the property name
    2. {
    3. Alert (Stu[s]);  //stu[s] is the property value corresponding to the S property in the Stu object.
    4. }

2) for-in Traversal array

    1. var array = [5,1,2,7];
    2. For (the index in array)
    3. {
    4. document.write (Array[index] + "<br/>");
    5. }


8. Adding methods to existing objects in JavaScript (using the prototype property)

For example: (for array classes, add Getmin () and Getmax () parties

    1. Array.prototype.getMax = function ()
    2. {
    3. var max = This[0];
    4. For (index in this )
    5. {
    6. if (This[index] > Max)
    7. {
    8. max = This[index];
    9. }
    10. }
    11. return Max;
    12. }
    13. Array.prototype.getMin = function ()
    14. {
    15. var min = This[0];
    16. For (index in this )
    17. {
    18. if (This[index] < min)
    19. {
    20. min = This[index];
    21. }
    22. }
    23. return min;
    24. }
    25. var array = [1,4,3,3,5,6];
    26. document.write (Array.getmax () + "<br/>");
    27. document.write (Array.getmin () + "<br/>");
    28. document.write ("<br/>");

JavaScript Notes _ Basic syntax

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.