函数

31 阅读1分钟

1.函数声明

function test() {}
 关键字   函数名
// 函数跟数组一样,栈里面是指向堆的地址,堆里面是内容
// 无论是变量名还是函数名都要遵循"小头风的规则", 如下
function theFirstName() {}
console.log(theFirstName)
输出为 function theFirstName() {}
 
// js不输出函数的地址,它输出地址指向那个房间的内容

2函数表达式

// 声明式
 function test1() {
     console.log("我是test1")
        }
 console.log(test1())
 
// 赋值式
  var test2 = function() {
     console.log("我是test2")
        }
  console.log(test2())

3.函数的参数(形参和实参)

  // 形参或者实参(可有可无), 真正的编程,有参数才更有意义
  // 小栗子
  function test(a, b ) { // 这里的a b是形参
  console.log(a + b);
  }
  test(1,2) // 这里的1 2 是实参
  输出为3