函数基础

  • 耦合
  • 高内聚,低耦合 -> 模块的单一责任制
  • 解耦合

    函数种类

  • 最基本的函数写法 - 函数声明

    1. function test(参数){
    2. 函数的执行语句;
    3. }
    4. // 被调用才会执行
    5. test();
  • 匿名函数表达式 函数字面量

    1. var test = function(){ -> 匿名函数
    2. 函数的执行语句;
    3. }

    形实参及映射

  • 形参 -> 占位 实参

  • 形参和实参数量可不等
  • arguments 实参
  • arguments.length // 实参个数 函数名.length // 形参个数 ```javascript function test(a, b) { a = 3; console.log(arguments[0]); // 3 函数可以改变参数 } test(1,2);

function test(a, b) { b = 3; console.log(arguments[1]); // undefined 实参未被定义 } test(1);

// 总结:给形参赋值的,可以改变形参,未给形参赋值的,改变形参没用

function test(a, b) { a = 3; // a 存在栈内存中 console.log(arguments[0]); // arguments 存在堆内存中 所以a和arguments[0]不是同一个值,映射关系 } test(1,2);

  1. - return 作用:1.终止函数执行 2.返回相应的值
  2. ```javascript
  3. function test() {
  4. console.log('执行完成');
  5. // return; 没写JS引擎隐式添加return
  6. }
  7. test();

变量类型

  • 全局变量 局部变量 [[scope]]
  • 函数: 一个固定的功能或者是程序段被封装的过程,实现一个固定的功能或是程序,在这个封装体中需要一个入口和一个出口,入口就是参数,出口就是返回

函数参数默认值

  • 初始化参数 默认值:undefined
  • es6 ```javascript function test(a = 1, b) { console.log(a); console.log(b); }

test(undefined, 2); // 1,2

function test(a = undefined, b) { console.log(a); console.log(b); // a 1 // arguments[0] }

test(1, 2); // 1,2

//兼容写法 function test(a,b){ // var a = arguments[0] || 1; // var b = arguments[1] || 2; var a = typeof(arguments[0]) !== ‘undefined’ ? arguments[0] : 1; var b = typeof(arguments[1]) !== ‘undefined’ ? arguments[1] : 2; }

  1. <a name="vw6Dg"></a>
  2. ## 递归
  3. ```javascript
  4. // n的阶乘 不使用for循环 递归
  5. // fact(n)
  6. // n! = n * (n-1)! 规律
  7. // 出口
  8. function fact(n){
  9. if(n === 1) {
  10. return 1;
  11. }
  12. return n * fact(n-1);
  13. }
  14. 递归:
  15. 找到规律
  16. 找到出口

预编译

  • // 1. 检查通篇的语法错误
  • // 1.5 预编译的过程
  • // 2. 解释一行,执行一行 ```javascript test(); function test(){ console.log(1); }

console.log(a); // undefined var a = 10;

函数声明整体提升 变量只有声明提升,赋值不提升

var a = 1; 2个步骤: 声明变量a, 赋值

  1. <a name="xl3Cz"></a>
  2. ## 暗示全局变量
  3. ```javascript
  4. var a = 1;
  5. b = 2;
  6. window = {
  7. a : 1,
  8. b : 2
  9. }
  10. function test(){
  11. var a = b = 1;
  12. }
  13. test();
  14. console.log(a); //报错
  15. window.b // 1
  1. function test(a) {
  2. console.log(a); // function a(){}
  3. var a = 1;
  4. console.log(a); // 1
  5. function a(){};
  6. console.log(a); // 1
  7. var b = function(){}
  8. console.log(b); // function(){}
  9. function d(){}
  10. }
  11. test(2);
  12. // AO activation object 活跃对象 函数上下文
  13. // 1 寻找形参和变量声明
  14. // 2 实参值赋值给形参
  15. // 3 找函数声明,赋值
  16. // 4 执行
  17. AO = {
  18. a: undefined -> 2 -> function a(){} -> 1
  19. b: undefined, -> function(){}
  20. d: function d(){}
  21. }

重点:
// AO activation object 活跃对象 函数上下文
// 1 寻找形参和变量声明
// 2 实参值赋值给形参
// 3 找函数声明,赋值
// 4 执行

// GO global object 全局上下文
// 1 找变量
// 2 找函数声明
// 3 执行
image.png

  1. var a = 1;
  2. function a() {
  3. console.log(2);
  4. }
  5. console.log(a); // 1
  6. // GO global object 全局上下文
  7. // 1 找变量
  8. // 2 找函数声明
  9. // 3 执行
  10. GO = {
  11. a: undefined -> function a(){} -> 1;
  12. }
  13. // GO === window
  14. console.log(a, b); // function a(){} undefined
  15. function a(){}
  16. var b = function(){}
  17. GO = {
  18. b: undefined,
  19. a: function a(){}
  20. }
  1. function test(){
  2. var a = b = 1;
  3. console.log(a); // 1
  4. }
  5. test();
  6. GO = {
  7. b: 1
  8. }
  9. AO = {
  10. a: undefined -> 1
  11. }
  1. var b = 3;
  2. console.log(a); // function a(a){...}
  3. function a(a){
  4. console.log(a); // function a(){}
  5. var a = 2;
  6. console.log(a); // 2
  7. function a(){}
  8. var b = 5;
  9. console.log(b); // 5
  10. }
  11. a(1);
  12. GO = {
  13. b: undefined
  14. a: function a(){...}
  15. }
  16. AO = {
  17. a: undefined
  18. 1
  19. function a(){}
  20. 2
  21. b: undefined,
  22. 5
  23. }
  1. a = 1;
  2. function test(){
  3. console.log(a); // undefined
  4. a = 2;
  5. console.log(a); // 2
  6. var a = 3;
  7. console.log(3); // 3
  8. }
  9. test();
  10. var a;
  11. GO = {
  12. a: undefined,
  13. 1
  14. test: funtion test(){...}
  15. }
  16. AO = {
  17. a: undefined
  18. 2
  19. 3
  20. }
  1. funtion test(){
  2. console.log(b); // undefined
  3. if(a){
  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. 1
  16. test: funtion test(){...},
  17. c: 3
  18. }
  19. AO = {
  20. b: undefined
  21. }

题目:
image.png function a(){}
image.png 2
image.png