51 函数
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
{
console.log("我是第一行");
console.log("我是第二行");
console.log("我是第三行");
}
var fun = new Function("console.log('hello 这是第一个函数');");
fun();
fun.hello = "你好";
function fun2(){
console.log("hello 这是第二个函数");
alert("哈哈哈");
document.write("啦啦啦");
}
fun2();
function(){
console.log("我是匿名函数");
}
var fun3 = function(){
console.log("我是匿名函数");
};
fun3();
</script>
<style>
</style>
</head>
<body>
</body>
</html>
52 函数的参数
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
function sum(a, b){
console.log(a + b);
}
sum(1, 1);
sum(123, "hello");
sum(true, false);
sum(123, 456, true, null);
sum(123);
</script>
<style>
</style>
</head>
<body>
</body>
</html>
53 返回值
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
function sum(a, b, c){
var d = a + b + c;
return d;
}
var res = sum(4, 8, 9);
var result = alert("hello");
</script>
<style>
</style>
</head>
<body>
</body>
</html>
54 练习
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
function judge(a){
return a % 2 == 0;
}
var res = judge(2);
function s(r){
return r * r * Math.PI;
}
function sayHello(name, age, gender, address){
console.log("我是 "+ name + ",今年我" + age + "岁了," + "我是一个" + gender + "人," + "我住在" + address);
}
sayHello("猪八戒", 28, "男", "高老庄");
var obj = {
name: "孙悟空",
age: 18,
gender: "男",
address: "花果山"
};
function sayHello(o){
console.log("我是 "+ o.name + ",今年我" + o.age + "岁了," + "我是一个" + o.gender + "人," + "我住在" + o.address);
}
sayHello(obj);
function fun(a){
console.log(a);
}
fun(sayHello);
fun(function(){alert("hello")})
fun(s);
fun(s(10));
</script>
<style>
</style>
</head>
<body>
</body>
</html>
55 return
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset = "utf-8">
<script type="text/javascript">
function fun(){
var obj = {name: "沙和尚"};
return obj
}
var a = fun();
console.log(a.name);
function fun2(){
function fun3(){
alert("我是fun3");
}
return fun3;
}
a = fun2();
a();
fun2()();
</script>
<style>
</style>
</head>
<body>
</body>
</html>