part 2
callee实现匿名函数的递归调用
var factorial = function(x){
if(x<= 1) return 1;
return x * arguments.callee(x-1);
}
闭包:数据的共享
function constfuncs(){
var funcs = [] ;
for(var i = 0; i<10; i++){
funcs[i]=function(){console.log(i);return i;}
}
return funcs;
}
var funcs = constfuncs();
funcs[5](); // 10 i的值被共享,并没有被回收
Function()构造函数
//Function()构造函数所创建的函数并不是使用词法作用域,函数体代码的编译总是会在顶层函数执行
var scope = "global";
function constructFunction(){
var scope = "local";
return new Function("return scope"); //无法捕获局部zuo yong yu作用域
}
//这一行代码返回global,因为通过Function()构造函数,所返回的函数使用的不是局部作用域
constructFunction(); // => "global"
//
类,原型,构造函数
我认为这个图很牛,值得保存。