<!DOCTYPE html><html lang="en"> <head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><title></title></head> <body> </body> <script>var name = '张三';(function () {if (typeof name == 'undefined') {var name = '李四';console.log(name);} else {console.log('hello' + name);}})()</script></html> 主要的疑虑就是为什么取不到外部的值,以及这个函数前后两个括号各代表的是什么意思?这样会形成闭包吗? 劳烦哪位大佬给我解答,谢谢了~~
1 回答
![?](http://img1.sycdn.imooc.com/533e4ce900010ae802000200-100-100.jpg)
慕工程0101907
TA贡献1887条经验 获得超5个赞
简单点,先去掉判断,这样写:
<script>var name = '张三';function test(){var name1 = name; console.log(name1); } test();</script>
上面看懂了,再加上判断即可。
添加回答
举报
0/150
提交
取消