Day41:JS basic method

1:js lesson:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 
 7 <!--    <script src="tests.js"></script>-->
 8 </head>
 9 <body>
10 
11 
12 
13 
14 
15     <script>
16 // <!--    alert(123)-->
17 //         var x;
18 //         x=12;
19 
20         // var y=22;
21         // alert(x)
22         // console.log(x);
23         // console.log(y);
24 
25         /*var name='liukeyu',age=28;
26         myFirstName="liu";
27         MyFirstName="liu"
28         // 1 present int,s present string
29         var 1MyTestValue=0,sMySecondValue='hi';*/
30 
31         // function f() {
32         //
33         // }
34 
35         console.log(true+1)
36         console.log(false+1)
37         // if([1,2,3]){
38         //     alert(111)
39         // }
40         // else{
41         //     alert(222)
42         // }
43      // if(null){
44      //        alert(111)
45      //    }
46      //    else{
47      //        alert(222)
48      //    }
49 // #########################Undefined:undefined
50         var x;
51         console.log(x);
52 
53         function f() {
54           document.write("<h1>hello yuan<h1>")
55         }
56         // f()
57         console.log(f());
58         console.log(typeof f());
59 
60 // ########################Null:null   for the object:
61 //         var perdon=new Person();
62 //         var person=null
63 
64 //NAN:not a number:belonging to Number:
65         console.log("hello"+2+true);
66         console.log(parseInt(3.123));//3
67         console.log(parseFloat('3'));//3
68         console.log(parseInt("123abc"));//123
69         console.log(parseInt("hello"));//NAN
70         console.log(parseInt("abc1234"));//NAN
71         console.log(parseInt(eval("1+1")))
72 
73 // typeof:distinguish the basic types:
74         console.log(typeof 123);
75         console.log(typeof "hello");
76         console.log(typeof true);
77         console.log(typeof null);//object
78         console.log(typeof undefined);
79 
80         console.log(typeof [12,33,44]);//object
81         console.log(typeof {"name":'yuan'});//object
82 
83  //++i --i i++ i--
84         var i=10;
85        console.log(i++);//10
86        console.log(++i);//12
87        console.log(i--);//12
88        console.log(--i);//10
89 
90 //&& || !
91         console.log("hello"&&4)//4
92     </script>
93 </body>
94 
95 </html>
View Code

2:JS contrlo sentance:

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>Title</title>
 6 </head>
 7 <body>
 8 
 9 <p>hello liu</p>
10 <p>hello zita</p>
11 <p>hello chen</p>
12 <div>divdivdivdivdiv</div>
13 <script>
14 //if else
15     // var name="alex";
16     // if (name=="liu"){
17     //     alert(666);
18     // }
19     // else if(name=="chen"){
20     //     alert(777);
21     // }
22     // else{
23     //     alert(888);
24     // }
25 
26  //switch;
27  //   var week=1;
28  //   switch (week) {
29  //        case 1:alert("星期一");break;
30  //        case 2:alert("星期2");break;
31  //        case 3:alert("星期3");break;
32  //        case 4:alert("星期4");break;
33  //        case 5:alert("星期5");break;
34  //        case 6:alert("星期6");break;
35  //        case 7:alert("星期7");break;
36  //        default:alert("nothing");
37  //   }
38 
39 //for&while ciruclation:
40 //       for(var i=0;i<10;i++) {
41 //           document.write("welcome"+i+"<br>")
42 //       }
43 
44       // var attr=[11,22,33];
45       // for(var i=0;i<attr.length;i++){
46       //     document.write(i);
47       //     document.write(attr[i]+"<br>")
48       // }
49       //
50     // for (i in attr){
51     //       document.write(i);
52     //       document.write(attr[i]+"<br>")
53     // }
54 
55     // var else_p=document.getElementsByTagName('p')
56     // console.log(else_p)
57     // // for(i in else_p){
58     // //     console.log(i)
59     // // }
60     //
61     // for (var i=0;i<else_p.length;i++){
62     //     console.log(i);
63     //     console.log(else_p[i])
64     // }
65 
66 //while:
67 //     var i=1,sum=0;
68 //     while (i<101) {
69 //         sum += i;
70 //         i++;
71 // }
72 //     console.log(sum)
73 
74 //unnormal:
75 
76     // try{
77     //     console.log(123);
78     //     throw Error("define error")
79     // }
80     // catch(e){
81     //     console.log(e)
82     // }
83     // finally {
84     //     console.log("finally")
85     // }
86 
87 </script>
88 </body>
89 </html>
View Code

3:JS_object:

  1 <!DOCTYPE html>
  2 <html lang="en">
  3 <head>
  4     <meta charset="UTF-8">
  5     <title>Title</title>
  6 </head>
  7 <body>
  8 <script>
  9 //two methods of creating a string object
 10     var str1="hello";
 11     // var str2=new String("hello2");
 12     // console.log(typeof str1);//string
 13     // console.log(typeof str2);//object
 14 
 15 //attribute of string
 16 //     console.log(str1.length);
 17 
 18 //method of string
 19     //range method
 20     // console.log(str1.italics());//<i>hello</i>
 21     // console.log(str1.bold());
 22     // console.log(str1.anchor());
 23 
 24     //x.charAt(index)
 25     // var str='welcome to the world of JS'
 26     // var str3=str.indexOf('l');
 27     // var str4=str.lastIndexOf('l')
 28     // alert(str3);//2
 29     // alert(str4);//18
 30 
 31     // console.log(str1.substr(1,3));//ell
 32     // console.log(str1.substring(1,3));//el
 33 
 34     // console.log(str1.slice(1,4));
 35 
 36     //replace,split,concat
 37 //-----------------------------------------------Array:
 38     //create aarray
 39     arr1=[1,'hello',[11,22],{'name':'liu'}];
 40     // arr2=new Array(4);
 41     // arr3=new Array(1,'world',true,[1,2,3]);
 42     // console.log(typeof arr1);//object
 43     // console.log(typeof arr2);//object
 44     // console.log(typeof arr3);//object
 45     //
 46     // var arr4=new Array(10);//it will regard the 10 is the size of the array;
 47     //
 48 //two dimension array:
 49 //     var week=new Array(7);
 50 //     for (var i=0;i<=6;i++){
 51 //         week[i]=new Array(2);
 52 //     }
 53 // week[0][0]='xingqitian';
 54 // week[0][1]='sunday'
 55     // ......
 56 
 57     // console.log(arr1.length);
 58 //join:
 59 //     var arr5=[1,2,3,4,'234'];
 60     // console.log(arr5.join('**'));//1**2**3**4**234,when python just for the string type;
 61 //Tostring:
 62 //     ret2 = arr1.toString();
 63 //     console.log(typeof ret2);//string
 64 //     console.log(ret2)//1,hello,11,22,[object Object]
 65 //concat
 66 //     var arr5=[1,2,3];
 67 //     var ret3=arr5.concat([5,66]);
 68 //     console.log(ret3);//(5) [1, 2, 3, 5, 66]
 69 //     console.log(typeof ret3);//object
 70 //
 71 //reverse
 72 //     var arr6=[22,33,100,44];
 73 //     console.log(arr6.reverse());//(4) [44,100, 33, 22]
 74 //     console.log(arr6.sort());//(4) [100, 22, 33, 44],base on the first bite
 75 //         function f(a,b) {
 76 //     if(a>b){
 77 //         return 1
 78 //     }
 79 //     else if(a<b){
 80 //         return -1
 81 //     }
 82 //     else{
 83 //         return 0
 84 //     }
 85 //         }
 86 //     console.log(arr6.sort(f))//(4) [22, 33, 44, 100]
 87 //     function f2(a,b) {
 88 //         return a-b
 89 //     }
 90 //     console.log(arr6.sort(f2));//(4) [22, 33, 44, 100]
 91 //slice
 92 //x.splice(start,deleteCount,value,value ...)//delete and insert
 93 
 94 //push pop:栈操作
 95 //     var arr7=[1,2,3];
 96 //     arr7.push([7,8,9]);
 97 //     console.log(arr7);//(4) [1, 2, 3, Array(3)]
 98 //     console.log(arr7.length)//4
 99 //     arr7.push('hello',5);
100 //      console.log(arr7);//(6) [1, 2, 3, Array(3), "hello", 5]
101 //     console.log(arr7.length)//6
102 //
103 //     console.log(arr7.pop());//5
104 //     console.log(arr7.length)//5
105 
106 //shift unshift:栈操作
107 //     var arr8=[4,5,6];
108 //     arr8.unshift([11,22,33]);
109 //     console.log(arr8);//(4) [Array(3), 4, 5, 6]
110 //     console.log(arr8.length);//4
111 //     arr8.shift();
112 //     console.log(arr8);//(3) [4, 5, 6]
113 //     console.log(arr8.length);//3
114 
115 //-------------------------------------------------function:
116    //method 1:
117    //  function f(x,y){
118    //      alert(123);
119    //      return x+y
120    //  }
121    //  console.log(f(32,566));
122    //  console.log(f.length)   //2
123 //面试题
124 //     function f(x,y) {
125 //     return x+y
126 //     }
127 //     var f=1;
128 //     var b=2;
129 //     console.log(f(f,b))//js_object.html?_ijt=g5g8sb82u0gca2lqc7rc13rfv7:129 Uncaught TypeError: f is not a function
130 
131 //arguments
132 //     function f(x,y){
133 //         console.log(arguments[0]);
134 //         console.log(arguments[1]);
135 //         console.log(arguments[2]);
136 //         console.log(arguments[3]);
137 //         return x+y
138 //     }
139 //     f(1,3,5,20);
140 
141     // function ADD() {
142     //     var sum=0;
143     // for(var i=0;i<arguments.length;i++){
144     //     sum+=arguments[i]
145     //     }
146     // return sum;
147     // }
148     // console.log(ADD(1,22,33))
149 
150 //anonymous function:
151 //     func('hello');//js_object.html?_ijt=g5g8sb82u0gca2lqc7rc13rfv7:151 Uncaught TypeError: func is not a function
152     // var func=function(arg){
153     //     alert(arg)
154     // };
155     // func('hello')//alert
156 
157        (func=function(arg){
158         alert(arg)
159     })('YUAN')
160 </script>
161 </body>
162 </html>
View Code

猜你喜欢

转载自www.cnblogs.com/zxver/p/13189702.html