Skip to content

JS谜题-函数中的函数 #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sunmaobin opened this issue May 26, 2017 · 0 comments
Open

JS谜题-函数中的函数 #18

sunmaobin opened this issue May 26, 2017 · 0 comments
Assignees
Milestone

Comments

@sunmaobin
Copy link
Owner

请问以下结果能否在点击button后,延迟3秒打印结果?

<html>
<body>
<button id="btn">Click</button>
<script src="jquery.js"></script>
<script>
(function(){
    var delayFun = function () {
       alert("This is sample function");
    };

    $("#btn").click(function(){
        setTimeout("delayFun",3000);
    });
})();
</script>
</body>
</html>

答案

不能。

原因

由于delayFun在一个匿名函数中定义,外部无法访问。而setTimeout已经脱离了当前执行环境,它的环境是window,所以3S后从window中找不到delayFun。

延伸

如果这里去掉setTimeout,即:

$("#btn").click(function(){
    delayFun();
});

点击button能立即打印结果吗?

答案:可以。

原因:click回调函数是在最外层的匿名函数中,也就是函数中的函数(即:闭包),闭包有访问外层函数中变量的权利。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant