函数

函数

一、函数定义

(一)、函数声明

1.注意打印函数名,会返回整个函数
function testFirsthello(){
    document.write("hello");
}
document.write(testFirsthello);

在这里插入图片描述

(二)、函数表达式

一、命名函数表达式
1.形式样子
var test=function test(){
    document.write("hello");
}
var test=function abc(){
    document.write("hello");
}
2.前后名字不一致,看前面
1.正确
var test=function abc(){
    document.write("hello");
}
test();

在这里插入图片描述

2.报错
1. 充当表达式,不能作为正常函数体了,所以abc报错
var test=function abc(){
    document.write("hello");
}
abc();

在这里插入图片描述

3. 函数名
1.第一种类型
var test=function abc(){
    document.write("hello");
}
// test();
document.write(test.name);

在这里插入图片描述

2.第二种类型
function testFirsthello(){
    document.write("hello");
}
// document.write(testFirsthello);
document.write(testFirsthello.name);

在这里插入图片描述

3.第三种类型
var test=function(){
    document.write("hello");
}
document.write(test.name);

在这里插入图片描述

匿名函数表达式—函数表达式(常用)
var test=function (){
    document.write("hello");
}

二、组成形式

1.基础:函数名称、参数(形参)(实参)、返回值

//形式参数
function sum(xx,xx,....){
	xxx
}
//实际参数
sum(xx,xx);
参数个数无所谓可以形参多也可以实参多
arguments->[1,2,3,4,2,1,3…],实参列表

2.不定参数求和

function sum(){
    var result=0;
    for(var i=0;i<arguments.length;i++){
        result+=arguments[i];
    }
    document.write(result);
}
sum(1,2,3,4,5,6,7,8,9);

在这里插入图片描述

3.arguments和形参的关系–之间有一一映射的关系,你变我就变

(1)a变,arguments变
// An highlighted block
function sum(a,b){
    a=2;
    console.log(arguments[0]);
}
sum(1,2);

在这里插入图片描述

(1)arguments变,a变
// An highlighted block

function sum(a,b){
    arguments[0]=2;
    console.log(a);
}
sum(1,2);

在这里插入图片描述

3.形参个数>实参个数

// An highlighted block
function sum(a,b){
    console.log(arguments[1]);
}
sum(1);

在这里插入图片描述

4.return—(1).终止函数(后面的不看了)(2).返回值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值