doraemon的python js入门必了解之一
### 11.8 js #### 11.8.1 js的引入方式 ```html <script type="text/javascript" src="js/index.js"></script> 放在哪个位置都生效 ``` #### 11.8.2 字符类型 递增和递减运算符: ```html <script> var a = 4; var c = a++; console.log(a); 5 console.log(c); 4 //这是因为c=a++是先将a的值赋给c再进行++ var a = 4; var c = ++a; console.log(a); 5 console.log(c); 5 //这是因为c=++a是先进行++再讲a的值赋给c </srcipt> ``` 字符串的拼接: ```html <script> var name='liujia',age=18; var srt = `${name}今年是${age}岁了` console.log(str) </script> ``` 数组,就是python中的列表 ```html <script> var arr =[1,'2','liujia']; //var声明标量,会把var声明的变量提升到全局作用域 for(i=0;i<arr.length;i++); console.log(arr[i]); function fn(){ var a = 4; } </script> ``` 数组的常用方法 ```html <script> var num = 123; var arr = ['red','green'.'yellow']; console.log(Array.isArray(arr)); console.log(arr.toString());//red,green,yellow console.log(num.toString());//将数字转化成字符串 console.log(typeof num.toString()) //检查类型 console.log(arr.join('#')) //将arr中的元素以#链接起来 console.log(arr.push('bule'));//返回了数组最新的长度 console.log(arr.pop());//返回删除的内容 console.log(arr.unshit('gray','black'));//添加内容 console.log(arr.shit('liuji/a'));//添加内容 var.splice();//对数组进行添加,删除,替换 var.slice(1) //对数组进行分割 </script> ``` if 和switch ```html <script> var score = 100; if(score > 80){ console.log('可以吃鸡') }else if(score <60){ sonsole.log('在家呆着学习') } var weather = prompt('请输入今天的天气'); switch(weather){ case'晴天': console.log('可以出去打篮球'); break; case'下雨': console.log('可以去上网'); break; default: console.log('学习'); break; } </script> ``` 比较 ```html <script> var a = 2; var b = '2'; console.log(a==b); true//比较的是值 console.log(a===b);false//比较的是值和数据类型 </script> ``` 循环 ```html <script> var arr = [8,9,,0]; for(var i = 0;i<arr.length;i++) console.log(arr[i]); var a = 1; while(a<=100){ console.log(a); a+=1 } </script> ``` 函数 ```html <script> function fn(){ switch(arguments.length); case 2: console.log('两个参数'); break; default: break; } fn(2,3,4) </script> ``` 对象: ```html <script> //1.字面量创建方式 car obj = {}; obj.name = 'liujia'; obj.fav = function(){ sonsole.log(this); } obj.fav(); console.log(obj.name); //2.构造函数 var obj2 = new Object(); console.log(obj2); obj.name = 'liudanni'; function add(x,y){ console.log(this.name); console.log(x); console.log(y); } console.dir(add); add(); add call(obj,1,2) // 3.像python中的类 class Person{ constructor(name,age){ this.name = name; this.age = age; } fav(){ console.log(this.name) } } var p =new Person('liujia',18); p.fav(); </script> ``` 日期对象: ```html <script> var date = new Date(); console.log(date); console.log(date.getDate()); console.log(date.getMonth()+1); console.log(date.getFullYear()); consloe.log(date.getDay()); console.log(date.getHours); console.log(date.getMinutes()); console.log(date.getSeconds()); console.log(date.toLocaleString()); </script> ``` 数字时钟案例 ```html <body> <h2 id = "time"></h2> </body> <script> var timeObj = document.getElementById('time'); sonsole.log(time); function getNowTime(){ var time = new Date(); var hour = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); var temp = "" + ((hour>12)?hour-12:hour); if (hour == 0){ temp = "12"; } temp += ((minute<10)?":0":":") + minute; temp += ((second<10)?':0':":") + second; temp += ((hour >= 12)?"PM":"AM"); timeObj.innerText = temp; } setInterval(getNowTime,20) </script> ``` Math对象 ```html <script> //求最大最小值 var values = [1,22,33,44,55,66,8]; var max = Math.max.apply(null,valuse); console.log(max); var a = 1.49999999999999 console.log(Math.ceil(a)); //天花板函数 console.log(Math.floor(a)); //地板函数 console.log(Math.round(a)); //四舍五入 console.log(Math.random()); 随机数 </script> ```