扫码关注官方订阅号
var test1 = function() { console.log('test1'); } var test2 = function() { console.log('test2'); } (function() { return test1; });
为什么输出结果是test2
走同样的路,发现不同的人生
因为分号的原因,语句被解释成了这样:
var test1 = function() { console.log('test1'); } var test2 = function() { console.log('test2'); }(function() { return test1; });
相当于执行了一次test2函数,把分号补全就正常了。
test2
不用写分号,这么写能输出test1
var test1 = function() { console.log('test1'); } var test2 = function() { console.log('test2'); } -function() { return test1; }()()
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
扫描下载App
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
因为分号的原因,语句被解释成了这样:
相当于执行了一次
test2
函数,把分号补全就正常了。不用写分号,这么写能输出test1