js函数与各种简单例题

1、函数名称严格区分大小写
2、函数名称重复会产生覆盖(显示最下面的一个)
test();
function test(){
alert('this is a test');
}
函数的基本形式
test为名称 ()必须要写
 
function test1(){
alert('this is test1 function');
}
alert(test1());
弹框会弹两次,第一次为'this is test1 funciton'
第二次为undefined
 
 
function calc(num1,num2){
return(返回) num1+num2;
}
alert(calc(1,2));
显示为3
num1和num2参数(只做参考)
 
alert(window.calc(3,5)); 权重最高
 
alert(calc(1,2,3,4,5,6)); 显示为3 上面只有两个下面也只取两个
 
 
function calc1(num1=1,num2=2){
return num1+num2;
}
alert(calc1(3,6));
显示为9 当下面没有值时;上面去1和2
 
 
function calc1(num1,num2){
num1=num1||1;
num2=num2||2;
return num1+num2;
}
alert(calc1(3,6));
显示为9;当下面没值时显示为3;
 
 
实现默认参数的形式
例 function calc4(x,y){
x=x||0;
y=y||0;
return x+y;
}
 
 
function calc4(x,y){
if(x===undefined){
x=0;
}
y=y===undefined?0:y;
return x+y;
}
 
arguments[0] 参数中的第一个数字
arguments[1] 参数中的第二个数字
arguments.length 得到参数的个数
 
变量的作用域
 
var x=1;
function test(){
document.write('函数体内x的值为:'+x+'<br/>');
var x=19;
document.write('函数体内对x重新赋值,此时x的值为:'+x+'<br/>');
}
document.write('函数体外x的值为:'+x+'<br/>');
test();
document.write('x的值为:'+x+'<br/>');
 
 
第一个x的值为1
第二个x的值为undefined
第三个x的值位19
第四个x的值为1
 
 
全局变量和局部变量的区别
1、 全局变量:写在函数(x=1)和大括号外部(var x=1)的变量
作用域:从定义那一行开始,一直到末尾
2、局部变量 :写在函数(var x=1)和代码中的变量
作用域:从定义那一行开始,一直到大括号或者return
注:全局变量在声明后程序的各个部分都能可以使用,但局部变量只能在局部使用,函数内部会优先使用局部变量,在使用全局变量