函数
一、函数定义
(一)、函数声明
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");
}
document.write(test.name);
2.第二种类型
function testFirsthello(){
document.write("hello");
}
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变
function sum(a,b){
a=2;
console.log(arguments[0]);
}
sum(1,2);
(1)arguments变,a变
function sum(a,b){
arguments[0]=2;
console.log(a);
}
sum(1,2);
3.形参个数>实参个数
function sum(a,b){
console.log(arguments[1]);
}
sum(1);
4.return—(1).终止函数(后面的不看了)(2).返回值