函数是任何一种语言的核心
函数的语法:function functionname(arg0,arg1,arg2,...){语句}
函数的调用:使用函数名后面加圆括号 例:functionname();//不带参数 或者functionname(pams1,pams2);//带参数
函数的特点:
1、函数的返回值是根据函数体中 有无return语句来决定的 (不需要在函数声明时定义函数返回值类型)
2、arguments对象是用来操作函数的参数数组的。可以将arguments当作数组来叔用
//无参、无返回值的函数
function SimpleMethod() {
console.log('我是一个函数,我的参数个数是:' + arguments.length);
}
SimpleMethod();//我是一个函数,我的参数个数是:0
//带参数、无返回值的函数
function SayHello(name, message) {
console.log('SayHello参数个数为:' + arguments.length);
console.log(name + '说:' + message);
}
SayHello('bigboss', "请大家自觉打扫卫生!");//SayHello参数个数为:2
//无参 带返回值的函数
function GiveMeReturnValue() {
return 10;
}
console.log(GiveMeReturnValue());//10
//带参带返回值的函数
function SimpleMethodWithAgsAndRtnvalue(pms1, pams2) {
if (pms1 > pams2) {
return pms1;
} else {
return pams2;
//return后面的语句不会再执行
console.log('dasdf');//不会输出
}
}
var rtn = SimpleMethodWithAgsAndRtnvalue(10, 12);
console.log(rtn);//12
//没有被传递值的参数 会默认被赋值为undefined
(function TestMethod(argument) {
console.log(argument);//输出undefined
})();