函数
//var fun = new Function("console.log(Hello 这是我的第一个函数);");
//fun();function fun2() {console.log("这是我的第二个函数");alert("哈哈哈哈");document.write("~~~~(>_<)~~~~");
}//console.log(fun2);
//调用fun2
//fun2();var fun3 = function () {console.log("我是匿名函数中封装的代码");
};fun3();

1. 函数说明
- 函数也是一个对象
- 函数中可以封装一些功能(代码),在需要时可以执行这些功能(代码)
- 函数中可以保存一些代码在需要的时候调用
- 使用 typeof 检查一个函数对象时,会返回一个 function
2. 创建一个函数
- 在实际开发中很少使用构造函数来创建一个函数对象
- 创建一个函数对象
var fun = new Function("console.log(Hello 这是我的第一个函数);");
3. 调用函数
- 封装到函数中的代码不会立即执行
- 调用函数,函数对象()
var fun = new Function("console.log(Hello 这是我的第一个函数);");
fun();
4. 函数其他创建方法
- 使用 函数声明 来创建一个函数
/* 语法:* function 函数名(形参1,,形参2...形参N){* 语句...* }*/function fun2() {console.log("这是我的第二个函数");alert("哈哈哈哈");document.write("~~~~(>_<)~~~~");
}//console.log(fun2);
//调用fun2
//fun2();
- 使用函数表达式来创建一个函数
/* var 函数名 = function(形参1,,形参2...形参N){* 语句....* }*/var fun3 = function () {console.log("我是匿名函数中封装的代码");
};fun3();