I took the time to write this evening. I was on duty in the Lab Building and continued to update the basic knowledge in the second lesson.
Naming rules
- Readability
- Normative-these two things are not easy to say. They are actually quite simple, that is, the code to be written can be understood by others, and the meaning and intention of each sentence of code are clear; it facilitates post-project maintenance and secondary development.
- Hungarian naming method-type prefix, uppercase letters
For example, oDiv indicates an object, and getElementsByTagName is a good example.
Operator
- Arithmetic: + addition,-subtraction, * multiplication,/division, % modulo (widely used)
- Example: The color of the line is changed.
1 <script> 2 window. onload = function () {3 var oUl = document. getElementById ('div1 '); 4 var aLi = oUl. getElementsByTagName ('lil'); 5 var I = 0; 6 7 for (I = 0; I <al, li. length; I ++) {// changed to 8 if (I % 2 = 0) 9 {aLi [I]. style. background = '# ccc' ;}; 10 }; 11 }; 12 </script> 13
This was originally intended to write an unordered list of different row colors, the results showed that ali was not defined, read for a long time did not find, please look for it together.
1 <script> 2 var a = 456; 3 alert (parseInt (a/60) + 'Min' + a % 60 + 'second '); // The error here is also changed. Note: 4 </script>
- Value assignment: =, + =,-=, * =,/=, and % = As mentioned in the previous article about addition, add yourself to yourself and add another value; the first two operators are used more often.
- Link: <,>, <=,> = ,! = ,! = (Incomplete, his requirements are the strictest, and the data type should be the same)
- Logic: & and (conditions must be met at the same time), | or (if two or more of them are set to one ),! No (it is the inverse)
- Operator priority: parentheses -- the priority of the included items is increased, for example, (a + B)/2.
Program process control:
Judgment:If Condition Statement (No explanation)Switch Statement ()Tri-category
If you want to execute one of several code blocks, you can use the switch statement:
Syntax:Switch (n) {case 1: run code block 1 break case 2: run code block 2 break... default: If n is neither 1 nor 2, run this code}
Tri-category<Expression 1>? <Expression 2>: <expression 3> ;"? "Operator meaning: evaluate the value of expression 1 first. If it is true, execute expression 2 and return the result of expression 2. If the value of expression 1 is false, execute expression 3, returns the result of expression 3.
1 <script> 2 var a = 44; 3 a % 2 = 0? Alert ('even number'): alert ('singular ') 4 </script>
Loop:While loop for Loop
Jump out:Break terminates the entire cycle. continue to terminate this cycle and continue the following cycle.
1 <script> 2 var i=0; 3 for(i=0;i<4;i++) 4 { 5 if(i==2){ 6 break; 7 }; 8 alert(i); 9 };10 </script>
What is truth and false?
<script> var a='2'; if(a){ alert('true') } else{ alert('flase') };</script>
This uses this applet to test what is true or false.
- True-true, non-null value, non-empty string, non-empty object
- False-flase, 0, null (null String), undefined, empty object
JSON:
JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript. JSON uses a completely language-independent text format, but it also uses a habit similar to the C language family (including C, C ++, C #, Java, JavaScript, Perl, Python, and so on ). These features make JSON an ideal data exchange language. Easy for reading and writing, and easy for machine parsing and generation. For example.
1 <script>2 var obj={a:1,b:3,c:'where',d:[4,5,6,7]};3 alert(obj.d[3]);4 </script>
1 <script>2 var obj={a:1,b:3,c:'where',d:[4,5,6,7]};3 var attr='';4 for(attr in obj)5 {6 alert(attr+'='+obj[attr]) 7 };8 </script>
The above are two small examples of jason. Is it much more convenient? This is the most basic usage. If you are interested, please continue to follow the article below.