Blogger Information
Blog 34
fans 0
comment 0
visits 21929
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月16日_ javascript变量、函数、流程控制、类型转换 - 九期线上班
只猫
Original
531 people have browsed it

一、js变量、函数声明

  1. //js变量声明 函数定义
  2. var num1 = 111;
  3. var num2 = 222;
  4. //js的输出
  5. //alert()系统弹窗 会阻塞浏览器运行
  6. //console.log()控制台输出 不阻塞浏览器运行
  7. function demo(){
  8. console.log(num1);
  9. console.log(num2);
  10. }
  11. demo();

二、流程控制

判断

  1. //流程控制
  2. function myfunc(){
  3. var score = 60;
  4. var myscore = 98;
  5. if(myscore>90){
  6. alert('优秀');
  7. }else if(myscore<=90 && myscore>70){
  8. alert('一般');
  9. }else if(myscore<=70 && myscore>=60){
  10. alert('及格');
  11. }else{
  12. alert('不及格');
  13. }
  14. }
  15. function myfunc2(){
  16. var score = 60;
  17. var myscore = 98;
  18. switch(true){
  19. case myscore>90:
  20. console.log('优秀');
  21. break;
  22. case myscore<=90 && myscore>=70:
  23. console.log('一般');
  24. break;
  25. case myscore<=70 && myscore>=60:
  26. console.log('及格');
  27. break;
  28. default:
  29. console.log('不及格');
  30. }
  31. }
  32. function myfunc3(){
  33. var score = 60;
  34. var myscore = 98;
  35. if (myscore>90) {
  36. return alert('优秀');
  37. }
  38. if (myscore<=90 && myscore>=70) {
  39. return alert('一般');
  40. }
  41. if (myscore<=70 && myscore>=60) {
  42. return alert('及格');
  43. }
  44. if (myscore<60) {
  45. return alert('不及格');
  46. }
  47. }

循环

  1. function loop1(){
  2. for(var i=0;i<10;i++){
  3. console.log('i='+i);
  4. }
  5. }
  6. function loop2(){
  7. var flag = 10;
  8. while(flag>0){
  9. flag--;
  10. console.log(flag);
  11. }
  12. }
  13. function loop3(){
  14. var flag = 10;
  15. do{
  16. flag--;
  17. console.log(flag);
  18. }
  19. while(flag>0);
  20. }

三、类型转换

parseInt 和 isNaN

  1. <input type="text" id="username" value="" placeholder="请输入您的年龄">
  2. <button onclick="save()">提交</button>
  3. <script type="text/javascript">
  4. function save(){
  5. var age = document.getElementById('username').value;
  6. age = parseInt(age);
  7. if(isNaN(age)){
  8. alert('年龄转换失败');
  9. return;
  10. }
  11. if(age > 100 || age < 0){
  12. alert('请确认您的年龄');
  13. return;
  14. }
  15. console.log(typeof(age));
  16. alert('您的年龄是'+age);
  17. }
  18. //类型转换
  19. var age = '20';
  20. console.log(typeof(age)); //string
  21. console.log(isNaN(age)); //false
  22. age = parseInt(age);
  23. console.log(typeof(age)); //number
  24. console.log(isNaN(age)); //false
  25. var age = 'abcd';
  26. console.log(isNaN(age)); //true
  27. var age = '111aaa';
  28. console.log(isNaN(age)); //true

课后总结:熟悉了js基础,和php有不同的地方,变量声明,数据类型。和php类似的地方,函数的定义和函数的调用、循环判断。两种语言都好好掌握。

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post