1.参数初始化及默认值

  1. function test(a, b){
  2. console.log(a);
  3. console.log(b);
  4. }
  5. test(1); // a = 1; b = undefined
  6. // 直接给形参赋值,是ES6的写法
  7. function test(a, b = 1){
  8. console.log(a);
  9. console.log(b);
  10. }
  11. test(1); // 1 1
  12. test(1, undefined); // 1, 1

1.设置参数默认值的方式

1.1选择 || 方法

  1. function test(a, b){
  2. var a = a || 1;
  3. var b = b || 2;
  4. var a = arguments[0] || 1;
  5. var b = arguments[1] || 2;
  6. console.log(a + b);
  7. }
  8. test(); // 3

2.typeof()

使用typeof(arguments[n])的方式,来检测其内部的参数是否为undefined,如果是undefined则代表没有传入参数,给它赋一个值

  1. function test(a, b) {
  2. var a, b;
  3. if(typeof(a) === 'undefined'){
  4. a = 1;
  5. }else{
  6. a = arguments[0];
  7. }
  8. if(typeof(b) === 'undefined'){
  9. b = 2;
  10. }else{
  11. b = arguments[1];
  12. }
  13. console.log(a + b);
  14. }
  15. test(); //
  16. // 三元运算符
  17. function test(a, b) {
  18. var a = typeof(a) === 'undefined' ? 1 : arguments[0];
  19. var b = typeof(b) === 'undefined' ? 2 : arguments[1];
  20. console.log(a + b);
  21. }

2.递归

递归: 函数自己调用自己
使用规律:
1. 找出规律

  1. 设置递归出口

3.预编译

  1. 检查通篇语法错误
  2. 预编译的过程
  3. 解释一行执行一行 ```javascript test(); // 1 // 函数声明整体提升 function test() { console.log(1); } // 变量只有声明提升 console.log(a); // undefined var a = 10;
  1. <a name="ufdXW"></a>
  2. # 4.暗示全局变量 imply global variable
  3. ```javascript
  4. var a = 1;
  5. b = 2;
  6. console.log(window.a); // a = window.a
  7. // b = window.b
  8. // window = {
  9. // a: 1,
  10. // b: 2
  11. // }
  12. // --------
  13. function test() {
  14. var a = b = 1;
  15. }
  16. test()
  17. console.log(window.a); // undefined
  18. console.log(window.b); // 1

5.AO activation object 活跃对象 函数上下文

  1. 先寻找形参和变量声明
  2. 实参值赋值给形参
  3. 找函数声明,赋值
  4. 执行 ```javascript
  5. 案列1 function test(a) { console.log(a); // f a(){} var a = 1; console.log(a); // 1 function a(){} console.log(a); // 1 var b = function(){} console.log(b); // f b(){} function d(){} }

test(2);

AO = { a: undefined => 2 => function a(){}=> 1 b: undefined d: function d(){} }

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

5.GO global object 全局上下文

  1. 找变量
  2. 找函数声明
  3. 执行
  4. GO === window
    1. console.log(a, b); // f a(){} undefined
    2. // 只有函数声明提升, 函数字面量(表达式)不会进行提升
    3. function a() {}
    4. var b = function (){}
    ```javascript 1.案例1 var b = 3; console.log(a); // function a(a){}

function a(a) { console.log(a); // f a() {} var a = 2; console.log(a); // 2

function a() {} var b = 5; console.log(b); // 5 }

a(1);

GO = { b: undefined a: f a(a){} }

AO = { a: undefined => 1 => f a(){} => 2

b: undefined => 5 }

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

作业

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