1 参数默认值

1.1 初始化参数

  1. 默认值:undefined
  2. 形参可以直接给值

    1. //第一种情况
    2. function test(a = 1, b){
    3. console.log(a);
    4. console.log(b);
    5. }
    6. test(undefined,2);//1 , 2
    7. //第二种情况
    8. function test(a = undefined, b){
    9. console.log(a);
    10. console.log(b);
    11. }
    12. test(1,2);//1 , 2
    13. /*
    14. 总结:
    15. 谁不是undefined,找谁
    16. */
  3. 不支持es6的浏览器无法使用直接给形参设置默认值,解决方法:

    1. function test(a,b){
    2. var a = agruments[0] || 1;//传入是0怎么办?
    3. var b = arguments[1] || 2;
    4. console.log(a);
    5. console.log(b);
    6. //或者
    7. var a,
    8. b;
    9. if(typeof(arguments[0]) !== 'undefined'){
    10. a = arguments[0];
    11. }else{
    12. a = 1;
    13. }
    14. console.log(a + b);
    15. //或者 三元运算符 推荐!
    16. var a = typeof(argument[0] !== 'undefined' ? arguments[0] : 1);
    17. }

    2.递归

    2.1 原则:

  4. 慎用

  5. 性能优化问题

    2.2 重要因素

  6. 找到计算规律

  7. 找到出口

    3.预编译

    3.1 过程

  8. 检查通篇的语法错误

  9. 预编译的过程
  10. 解释一行,执行一行

    3.2 变量提升 ⭐

  11. 总结:函数声明整体提升,变量只有声明提升,赋值不提升

  12. 基础案例:


      1. //1.
      2. test(); //1 仍然可以打印
      3. function test(){
      4. console.log(1);
      5. }
      6. //2.
      7. console.log(a);//undefined 不会报错,但是打印不出10
      8. var a = 10;
      9. /*
      10. 总结:函数声明整体提升,变量只有声明提升,赋值不提升
      11. */
      12. //3
      13. console.log(a);//打印函数体
      14. function a(a){
      15. var a = 10;
      16. var a = function(){}
      17. }
      18. var a = 1;
  13. 函数上下文 AO activation object

    1. /*
    2. 1.寻找函数的形参和变量声明;
    3. 2.把实参的值赋给形参
    4. 3.寻找函数的声明,赋值函数体
    5. 4.执行函数
    6. */
    7. //1.
    8. function test(a){
    9. console.log(a);
    10. var a = 1;
    11. console.log(a);
    12. function a(){}
    13. console.log(a);
    14. var b = function(){}
    15. console.log(b);
    16. function d(){}
    17. }
    18. /*
    19. AO activation object
    20. 活跃对象,函数上下文
    21. 1.寻找函数的形参和变量声明
    22. AO = {
    23. a: undefined,
    24. b: undefined,
    25. }
    26. 2.把实参的值赋值给形参
    27. AO = {
    28. a: undefined -> 2,
    29. b: undefined,
    30. }
    31. 3.寻找函数的声明,赋值函数体
    32. AO = {
    33. a: undefined -> 2 -> function a(){} ,
    34. b: undefined,
    35. d: function d(){}
    36. }
    37. 4.执行函数
    38. AO = {
    39. a: undefined
    40. -> 2
    41. -> function a(){}
    42. -> 1,
    43. b: undefined -> function(){},
    44. d: function d(){}
    45. }
    46. */
    47. //2.
    48. function test(a, b){
    49. console.log(a); //1
    50. c = 0;
    51. var c;
    52. a = 5;
    53. b = 6;
    54. console.log(b); //6
    55. function b(){}
    56. function d(){}
    57. console.log(b); //6
    58. }
    59. test(1)
    60. /*
    61. AO = {
    62. a : undefined
    63. -> 1;
    64. b : undefined
    65. -> function b(){};
    66. c : undefined;
    67. d : function d(){};
    68. }
    69. */
  14. 全局上下文

    1. /*
    2. 1.寻找变量声明
    3. 2.寻找函数声明,赋值函数体
    4. 3.执行函数
    5. */
    6. //1.
    7. var a = 1;
    8. function a(){
    9. console.log(2);
    10. }
    11. console.log(a);//1
    12. //2.
    13. function test(){
    14. var a = b = 1;
    15. console.log(a);
    16. }
    17. test();
    18. /*
    19. GO = {
    20. b = 1;
    21. }
    22. AO = {
    23. a : undefine d -> 1;
    24. }
    25. */
    26. //3.
    27. var b = 3;
    28. console.log(a);//function(){}
    29. function a(a){
    30. console.log(a);//function(){}
    31. var a = 2;
    32. console.log(a);.//2
    33. function a(){
    34. var b = 5;
    35. console.log(b);//5
    36. }
    37. }
    38. a(1);
    39. /*
    40. GO = {
    41. b = 3;
    42. a = function(){}
    43. }
    44. AO = {
    45. a = undefined
    46. -> 1
    47. -> function(){};
    48. AO2 = {
    49. b = undefind
    50. -> 5;
    51. }
    52. }
    53. */
    54. //4.
    55. a = 1;
    56. function test(){
    57. console.log(a);//undefined
    58. a = 2;
    59. console.log(a);//2
    60. var a = 3;
    61. console.log(a);//3
    62. }
    63. /*
    64. GO = {
    65. a = 1;
    66. test = function();
    67. }
    68. AO = {
    69. a = undefined
    70. -> 2
    71. -> 3;
    72. }
    73. */
    74. //5.
    75. function test(){
    76. console.log(b);//undefined;
    77. if(a){
    78. var b = 2;
    79. }
    80. c = 3;
    81. console.log(c);//3
    82. }
    83. var a;
    84. test();
    85. a = 1;
    86. console.log(a);//1
    87. /*
    88. GO = {
    89. a = undefined;
    90. test = function(){}
    91. c = 3;
    92. }
    93. AO = {
    94. AO2 = {
    95. b = undefined; ⭐
    96. }
    97. }
    98. */

    3.3 暗示全局变量 imply global variable ❓

  15. 案例 ```javascript //1 a = 1; var b = 2; console.log(a);//1 console.log(b);//2 / 以上声明方式都将a和b放到 window = { a = 1, b = 2; } / //2. function test(){ var a = b = 1; } console.log(b); //1 为什么?为什么会提升到全局作用域

  1. <a name="dIJvX"></a>
  2. ### 3.4 作业 ❓
  3. ```javascript
  4. /* 第一题 */
  5. function test(){
  6. return a; //function
  7. a = 1;
  8. function a(){}
  9. var a = 2;
  10. }
  11. console.log(test());//function
  12. /*
  13. GO = {
  14. test = function test(){};
  15. a = 1;
  16. }
  17. AO = {
  18. a = undefined
  19. -> function(){}
  20. -> 2;
  21. }
  22. */
  23. /*第二题*/
  24. function test(){
  25. a = 1;
  26. function a(){}
  27. var a = 2;
  28. return a;
  29. }
  30. console.log(test());//2
  31. /*
  32. GO = {
  33. a = 1;
  34. }
  35. AO = {
  36. a = undefined
  37. -> function(){}
  38. -> 2;
  39. }
  40. */
  41. /* 第三题 */
  42. a = 1;
  43. function test(e){
  44. function e(){}
  45. arguments[0] = 2;
  46. console.log(e); //2
  47. if(a){
  48. var b = 3;
  49. }
  50. var c;
  51. a = 4;
  52. var a;
  53. console.log(b);//undefined
  54. f = 5;
  55. console.log(c);//undefined;
  56. console.log(a);//4
  57. }
  58. var a;
  59. test(1);
  60. /*
  61. GO = {
  62. a = undefined
  63. -> 1;
  64. test = test(e){};
  65. f = 5;
  66. }
  67. AO = {
  68. e = undefined
  69. -> 1
  70. -> function(){}
  71. -> 2;
  72. b = undefined;
  73. c = undefined;
  74. a = undefined
  75. -> 4;
  76. }
  77. */
  78. //4
  79. if(typeof(a) && (-true) + (+undefined) + ''){
  80. console.log("通过了");
  81. }else{
  82. console.log("没通过");
  83. }
  84. //5
  85. if(1 + 5 * '3' == 16){
  86. console.log("通过了");
  87. }else{
  88. console.log("没通过");
  89. }
  90. //6
  91. console.log(!!' ' + !!'' - !!false || '通过了');