1、什么是函数
实现特定功能的n条语句的封装体
只有函数是可以执行的,其他类型的不可以
2、为什么要函数
提高代码复用率
3、如何定义函数
函数声明
表达式
- function fun(){
- console.log('函数声明')
- }
- var fun2=function fun1(){
- console.log('表达式')
- }
4、如何调用(执行)函数
test():直接调用
obj.test():通过对象调用
obj.test():new调用
test.call()/test.apply(obj):类似obj.test(),临时让obj调用test方法
- function fun1(){
- console.log(this)
- console.log('函数声明,fun1')
- }
- fun1.apply(obj);//obj.fun1()
- var obj={name:'sd'}
- function fun1(){
- console.log(this)
- console.log('函数声明,fun1')
- }
- fun1.call(obj);//obj.fun1()
可以让任意一个函数成为对象的方法
