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:
- var x = 3;
- 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:
- if (x==4)//can perform comparison operations
- 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)
- var x = "99 multiplication table";
- alert (x);
- document.write ("<table>");
- for (var i=1; i<=9; i++)
- {
- document.write ("<tr>");
- For (var j=1; j<=i; J + +)
- {
- document.write ("<th>");
- document.write (j + "*" + i + "=" + i*j);
- document.write ("</th>")
- }
- document.write ("</tr>");
- }
- 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:
- Function Show ()
- {
- Alert ("show");
- alert (arguments.length);
- }
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
- var show = new Function ("x","y","return x+y;");
- alert (show);
3) anonymous functions (typically used to define how events are handled):
For example:
[JavaScript]View Plaincopy
- var show = function ()
- {
- Alert ("function run");
- }
- 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:
- function Myadd ()
- {
- var sum = 0;
- For (var x=0; x<arguments.length; x + +)
- {
- document.write ("Argu" + x + ":" + arguments[x] + "<br/>");
- Sum + = arguments[x];
- }
- return sum;
- }
- 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:
- var arr = [4,1,6,8]; //directly define arrays
- var arr = new Array (); //define an empty array
- for (var i=0; i<arr.length; i++)
- {
- Alert (Arr[i]);
- }
5, the definition of the object
- The definition here is a bit like a constructor.
- function person (name,age)
- {
- this.name = name;
- this.age = age;
- }
- var p = New Person ("Mingzi","1");
- P.getname = function () //tool method
- {
- return p.name; //or return p["name"];
- }
6. With statement
- 6) withstatement
- With (Stu) //stu is an object
- {
- Alert (name+"..." +age);
- }
7. For-in statement
1) for-in traverse the properties of the class
- For (S-in Stu) //s is the property name
- {
- Alert (Stu[s]); //stu[s] is the property value corresponding to the S property in the Stu object.
- }
2) for-in Traversal array
- var array = [5,1,2,7];
- For (the index in array)
- {
- document.write (Array[index] + "<br/>");
- }
8. Adding methods to existing objects in JavaScript (using the prototype property)
For example: (for array classes, add Getmin () and Getmax () parties
- Array.prototype.getMax = function ()
- {
- var max = This[0];
- For (index in this )
- {
- if (This[index] > Max)
- {
- max = This[index];
- }
- }
- return Max;
- }
- Array.prototype.getMin = function ()
- {
- var min = This[0];
- For (index in this )
- {
- if (This[index] < min)
- {
- min = This[index];
- }
- }
- return min;
- }
- var array = [1,4,3,3,5,6];
- document.write (Array.getmax () + "<br/>");
- document.write (Array.getmin () + "<br/>");
- document.write ("<br/>");
JavaScript Notes _ Basic syntax