函数的两种写法
//函数
var test = function(){
document.write('a')
}
function test2(a , b){//a,b形参
var c = a + b;
document.write(c)
}
形参实参
//函数
var test = function(){
document.write('a')
}
function test2(a , b){//a,b形参
var c = a + b;
document.write(c)
}
test();
test2(1,2);//1,2实参
argument
代指实参,有点像c++的指针
//arguments 实参
function test3(a,b,c) {
if (test3.length > arguments.length) {
document.write('形参多')
}else{
document.write('实参多')
}
}
test3(1,2,3,4)
还可以使用隐藏的实参
<script>
function test3(a,b,c) {
if (test3.length > arguments.length) {
document.write('形参多')
}else{
document.write('实参多')
document.write(arguments[3])
}
}
test3(1,2,3,4)
</script>
return
很重要
//return
function test4(a,b) {
a = a + b;
return a;
}
document.write(test4(1,2))
函数参数调用规则
函数里面可以调用外面的
函数外面不可以调用里面的
当外面调用里面时会报错
// 里面的可以访问外面的
//外面的不能访问里面的
var a = 123;
function test5(){
// 里面的可以访问外面的
// var p = 321;
document.write(a);
}
test5();
//函数外面的不能访问里面的
// document.write(p);