函数的参数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function sum(){
console.log(1+1);
}
sum();
function sum1(a,b){
console.log(a+b);
}
sum1(1,2);
sum1(123,"hello");
sum1(true,false);
sum1(123,23,24);
sum1(123)
</script>
</head>
<body>
</body>
</html>
函数的返回值
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function sum(a,b,c){
alert(a+b+c);
}
sum(1,2,3);
function sum1(a,b,c){
return a+b+c;
alert("这是一段死代码");
}
var result=sum1(2,3,4);
console.log(result);
console.log(sum1(1,2,3));
</script>
</head>
<body>
</body>
</html>
立即执行函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
(function(a,b){
console.log(a);
console.log(b);
})(1,2);
</script>
</head>
<body>
</body>
</html>
对象的属性值为函数时
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var obj=new Object();
obj.name="孙悟空";
obj.age=18;
obj.sayName=function(){
console.log(obj.name);
};
var fun=function(){
console.log(obj.name);
};
console.log(obj.sayName);
obj.sayName();
fun();
var obj2={
name:"主播",
age:18,
SayDay:function(){
console.log(obj2.name);
}
};
obj2.SayDay();
</script>
</head>
<body>
</body>
</html>
枚举对象中的属性
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var obj={
name:"孙悟空",
age:18,
gender:"男",
address:"花果山"
};
for(var n in obj){
console.log(n);
};
for(var n in obj){
console.log(obj[n]);
};
</script>
</head>
<body>
</body>
</html>
全局作用域(Scope)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
function fun(){
console.log("这是一个window对象的一个方法")
}
fun();
window.fun();
</script>
</head>
<body>
</body>
</html>
变量的声明提前
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
fun1();
function fun1(){
console.log("这是一个使用函数声明形式创建的函数");
}
fun2();
var fun2=function(){
console.log("这是使用函数表达式创建的函数");
}
</script>
</head>
<body>
</body>
</html>
函数作用域
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
var a=10;
function fun1(){
console.log("a="+a);
var a=20;
}
fun1();
function fun3(){
fun4();
function fun4(){
console.log("我是fun4");
};
};
fun3();
function fun5() {
a=10;
};
console.log(a);
var e=10;
function fun6(e) {
console.log(e);
};
fun6(20);
fun6();
</script>
</head>
<body>
</body>
</html>
|