设置参数的默认值(当函数调用时,实参传值则使用实参值,若不传值则使用默认值)

  1. 方法一
  2. function test(a,b) {
  3. var a = arguments[0] || 1, //若 a的值为undefined 则 执行后面的 赋值1
  4. b = arguments[1] || 2;
  5. console.log(a+b);
  6. }
  7. 方法二
  8. function test(a,b) {
  9. var a,b;
  10. if(typeof (arguments[0])!=='undefined'){ //此处需注意 typeof的返回值必定是string类型 所以判断的条件也要加''引号string类型
  11. a = arguments[0];
  12. }else{
  13. a = 1;
  14. }
  15. if(typeof (arguments[1])!=='undefined'){
  16. b = arguments[1];
  17. }else{
  18. b =2;
  19. }
  20. console.log(a,b);
  21. console.log(a+b);
  22. }
  23. 方法三
  24. function test(a,b) {
  25. var a = typeof(arguments[0])!=='undefined' ? arguments[0] : 1,
  26. b = typeof(arguments[1])!=='undefined' ? arguments[1] : 2;
  27. console.log(a+b);
  28. }

预编译过程

1.通篇的检查语法错误
1.5 预编译的过程
2.解释一行执行一行

2. 函数执行之前进行的步骤

  1. 创建 AO对象 -> AO activation object 活跃对象, 函数上下文 =>环境
    2. 寻找函数中的形参和变量声明
    3. 将实参赋值给形参
    4. 寻找函数体内函数声明 赋值函数体
    5. 执行(赋值等操作)

    3. 全局上下文 =>环境

  2. 创建 GO对象 -> 全局上下文 ===window
    2. 寻找变量声明
    3. 寻找函数声明
    4. 执行(赋值等操作)

例1

  1. function test() {
  2. var a = b = 1;
  3. console.log(a);
  4. }
  5. test(); // 1
  6. // GO {
  7. // b: 1 //执行test()时将 b挂载在全局变量
  8. // }
  9. // AO{
  10. // a: undefined 1 //寻找全局变量b 将b的值赋值给a
  11. // }
  12. 执行 1

例2

  1. function test(a) {
  2. console.log(a);
  3. var a = 1;
  4. console.log(a);
  5. function a() {};
  6. console.log(a);
  7. var b = function () {}
  8. console.log(b);
  9. function d() {};
  10. }
  11. test(2);
  12. // GO{
  13. // test: function test(a){}
  14. // }
  15. // AO{
  16. // a: undefined =>2 => function a(){}
  17. // b: undefined
  18. // d: function d(){}
  19. // }
  20. // 执行 function a(){} (a = 1) => 1 1 (b = function () {}) => function (){}
  21. // function a(){} 1 1 function (){}

例3

  1. function test(a,b) {
  2. console.log(a);
  3. c = 0;
  4. var c;
  5. a = 5;
  6. b = 6;
  7. console.log(b);
  8. function b(){};
  9. function d(){};
  10. console.log(b);
  11. }
  12. test(1);
  13. // GO{
  14. // test: function test(a,b){}
  15. // }
  16. // AO{
  17. // a: undefined 1 5
  18. // b: undefined function b(){} 6;
  19. // c: undefined 0
  20. // d: function d(){} ;
  21. // }
  22. // 执行 1 6 6
  23. // 1 6 6

例4

  1. var a = 1;
  2. function a(){
  3. console.log(2);
  4. }
  5. console.log(a);
  6. // GO{
  7. // a: undefined => function a(){} =>1
  8. // }

例5

  1. console.log(a,b);
  2. function a(){}
  3. var b = function(){}
  4. // go{
  5. // a: undefined function a(){}
  6. // b: undefined
  7. // }

例6

  1. var b = 3;
  2. console.log(a);
  3. function a(a){
  4. console.log(a);
  5. var a = 2;
  6. console.log(a);
  7. function a(){}
  8. var b = 5;
  9. console.log(b);
  10. }
  11. a(1);
  12. // go:{
  13. // b: undefined => 3
  14. // * a: function a(a){}
  15. // }
  16. // ao:{
  17. // a: undefined => 1 => function a(){} =>start 2
  18. // b: undefined =>start 5
  19. // }
  20. // * function a(a){} function a(){} 2 5

例7

  1. a = 1;
  2. function test() {
  3. console.log(a);
  4. a = 2;
  5. console.log(a);
  6. var a = 3;
  7. console.log(a);
  8. }
  9. test();
  10. var a;
  11. /*
  12. Go = {
  13. a : undefined =>1
  14. test: function test() {}
  15. }
  16. Ao = {
  17. a : undefined => 2 => 3
  18. }
  19. 函数里面找不到 找外面的 a 1 2 3 错误
  20. 函数里面有 a 只是初始化 undefined 正确的值 undefined 2 3
  21. 如果连undefined都没有去外面找
  22. */

例8

  1. function test() {
  2. console.log(b);
  3. if(a){
  4. var b = 2;
  5. }
  6. c = 3;
  7. console.log(c);
  8. console.log(a);
  9. }
  10. var a;
  11. test();
  12. a = 1;
  13. console.log(a);
  14. /*
  15. Go = {
  16. a : undefined
  17. }
  18. Ao = {
  19. b : undefined
  20. c : undefined 3
  21. }
  22. /*
  23. undefined 3 undefined 1

例9

  1. function test() {
  2. return a;
  3. a = 1;
  4. function a() {}
  5. var a = 2;
  6. }
  7. console.log(test());
  8. /*
  9. Go = {
  10. test: function test() {}
  11. }
  12. Ao = {
  13. a:undefined function a() {} =>start 2
  14. }
  15. function a() {}

例10

  1. function test() {
  2. a = 1;
  3. function a() {}
  4. var a = 2;
  5. return a;
  6. }
  7. console.log(test());
  8. /*
  9. Go = {
  10. test: function test(){}
  11. }
  12. Ao = {
  13. a: undefined => 1 function a(){} =>start 2
  14. }
  15. */

例11

  1. a = 1;
  2. function test(e) {
  3. function e() {}
  4. arguments[0] = 2;
  5. console.log(e);
  6. if(a){
  7. var b = 3;
  8. }
  9. var c;
  10. a = 4;
  11. var a;
  12. console.log(b);
  13. f = 5;
  14. console.log(c);
  15. console.log(a);
  16. }
  17. var a;
  18. test(1);
  19. console.log(a);
  20. console.log(f);
  21. /*
  22. Go = {
  23. test: function test(e){}
  24. a: undefined => start 1
  25. f: 5
  26. }
  27. Ao = {
  28. e: undefined 1 function e() {} 2
  29. a: undefined 4
  30. b: undefined
  31. c: undefined
  32. }
  33. zx 2 undefined undefined 4 1 5

面试题讲解

  1. var a = false + 1;//number(false) = 0+1
  2. console.log(a);//1
  3. var b = false == 1;
  4. console.log(b);//false
  5. if (typeof (a) && (-true) + (+undefined) + "") {
  6. console.log('通过了')
  7. } else {
  8. console.log("没通过")
  9. }
  10. // // typeof(a) ->"1" -> 非空-> true
  11. // // (-true) + (+undefined) + "" =>-1+NaN+""->"NaN" -> 非空 ->true
  12. if (1 + 5 * "3" === 16) {
  13. console.log("通过了");
  14. } else {
  15. console.log("没通过")
  16. }
  17. console.log(1 + 5 * "3")//16
  18. console.log(!!' ' + !!'' - !!false || '为通过')//1
  19. console.log(!!' ') //true
  20. console.log(!!'') //false
  21. console.log(!!false)//fasle
  22. window.a || (window.a = "1");
  23. console.log(window.a);//'1'
  24. //考察点括号 先执行右边括号里的内容

(4)暗示全局变量

  1. 暗示全局变量的问题 imply global variable
    2. 在全局上的时候 无论是var a = 1;或者a = 1都是放在window对象上的