js函数基础学习

五、函数

具有一定功能,封装成函数。

【函数三要素】

  • 函数名
  • 参数(形参,实参)
  • 返回值

声明函数

1、字面量创建(声明式函数)

function 函数名() {
   函数体}

2、构造函数

let 函数名 = new Function();
console.log(typeof 函数名);//function

3、函数表达式

let 函数名1 = function 函数名2(){
   }
函数名1();
//匿名函数表达式
let 函数名1 = function(){
   }
函数名1();

函数的调用:函数名();

返回值

在函数体里写return 表达式; 当函数没有返回值时会输出undefined

console.log(函数名());

function b(){
   
    return 1+2;
}
console.log(b());//3

参数

function c(a,b){
   
    return a+b;
}
console.log(c());//NaN
console.log(c(1,2));//3
形参:函数声明

1、形参个数多,多的形参值为undefined

function aa(a,b,c,d){
   
    console.log(a,b,c,d);
}
aa(1,2,3);//1 2 3 undefined
aa(1,2,3,4,5);//1 2 3 4

2、形参默认值:形参名=表达式

function
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值