Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Created March 12, 2016 06:46
Show Gist options
  • Select an option

  • Save black-black-cat/7111068dafab46f30a59 to your computer and use it in GitHub Desktop.

Select an option

Save black-black-cat/7111068dafab46f30a59 to your computer and use it in GitHub Desktop.
闭包问题
function fun(n,o) {
console.log(o)
return {
fun:function(m){
return fun(m,n);
}
};
}
var a = fun(0); a.fun(1); a.fun(2); a.fun(3);//undefined,0 ,0 ,0
var b = fun(0).fun(1).fun(2).fun(3);//undefined,0,1,2
// c.fun == function(m) {return fun(m,n)} , n的值通过闭包获得1
var c = fun(0).fun(1); c.fun(2); c.fun(3);//undefined,0,1,1
//问:三行a,b,c的console.log结果分别是什么?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment