函数语法
function 函数名(形参列表){
执行代码
}
函数的调用
function add (number1,number2){
var sum = number1 + number2;
return sum;
}
var a = add(3,4); //7
函数的参数 原始类型
实参小于形参个数
function add (number1,number2){//number1 = 3,number2 = undefined
var sum = number1 + number2;
return sum;
}
var a = add(3); //
实参大于形参个数
function add (number1,number2){
//number1 = 3,number2 = 4
var sum = number1 + number2;
return sum;
}
var a = add(3,4,5); //
实参有多个
function add (){
var length = arguments.length,
sum = 0,
paramenter;
for (var i = 0; i < length; i++) {
paramenter = arguments[i];
sum += paramenter;
}
return sum;
}
alert(add(3,4));
alert(add(3,4,5,6,7,8));
参数为对象类型 引用传递 值改变
function increment(person){
person.age += 1;
return person;
}
var jerry ={ name:'Jerry',age = 1; }
var x = increment(jerry);