函数声明
function test1( ){console.log(11111)}
函数表达式
var test2 = function ( ){console.log(2222)}
函数参数
function test(a, b){
// 相当于隐式的在函数内定义a变量和b变量
// arguments 实参列表 [11, 3, 4]
// test.length 形参数量
}
test(11, 3, 4)
有实参的形参和arguments相映射
function sum(a, b){
// a有实参,所以和arguments相映射
a = 2;
console.log(arguments[0]) // 2
arguments[0] = 3;
console.log(a) // 3
// b没有实参,所以不能和arguments相映射
b = 2;
console.log(arguments[1]) // undefined
}
sum(1)
闭包:当内部函数被保存到外部时,将会生成闭包,闭包会导致原有作用域链不释放,造成内存泄漏
立即执行函数:此类函数没有声明,在一次执行过后即释放。适合做初始化工作
只用函数表达式才能被执行负号执行
(function ( ){ } ( ))
(function ( ) { })( )
function test ( ){console.log('不能被执行')}( )
var test = function ( ) {console.log('可以被执行')})( )
console.log(test) // undefined
+ function test ( ){console.log('可以被执行')}( )
console.log(test) // 报错
- function test ( ){console.log('可以被执行')}( )
console.log(test) // 报错
! function test ( ){console.log('可以被执行')}( )
console.log(test) // 报错
arguments.callee
function test ( ){
console.log(arguments.callee) // 输出test函数体
console.log(arguments.callee == test) // true
}
test( )
实例:
var num = (function (n){
if(n == 1){
return 1;
}
return n * arguments.callee(n - 1)
}(100))
fun.caller
function test ( ){
demo( );
}
function demo ( ){
console.log(demo.caller) // 输出test函数体
}