Created
March 12, 2016 06:46
-
-
Save black-black-cat/7111068dafab46f30a59 to your computer and use it in GitHub Desktop.
闭包问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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