不完整的可执行自加载闭包

发布于 2024-11-18 05:16:13 字数 454 浏览 2 评论 0原文

浏览器/ajax/javascript是否可以自动完成,并关闭自加载闭包(如果它们保持打开状态..?)

1 (function()
2 {
3 function a(n){this.name=n;}
4 var a=this;
5 //more code in here
6 alert(a);
7 //})();


- The terminating brace and parens were not present in the script i saw
- Is it possible that it must be generated dynamically, so that the .js file
- does not contain it but the script/ajax later appends it ..?
- Can the browser complete it..?

Is it possible for browsers/ajax/javascript to autocomplete, and close the self-loading closures if they are left open ..?

1 (function()
2 {
3 function a(n){this.name=n;}
4 var a=this;
5 //more code in here
6 alert(a);
7 //})();


- The terminating brace and parens were not present in the script i saw
- Is it possible that it must be generated dynamically, so that the .js file
- does not contain it but the script/ajax later appends it ..?
- Can the browser complete it..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤千羽 2024-11-25 05:16:13

为什么不运行一些简单的测试呢?

使用这样的一些文件:

<script>
(function a () {
    alert('a');
</script>
<script>
$(function() { alert('b'); a(); });
</script>

这将告诉您以下场景:

  1. alerts ('a'),alerts('b'),alerts('a') ==> (不太可能):浏览器完成函数定义并编写闭包执行括号

  2. alerts('b'),alerts('a') ==>;浏览器完成了函数定义并且调用按预期进行

  3. alerts('b'), throws error for undefined a() ==>;浏览器忽略不完整的函数定义

  4. 什么都不做==>浏览器在找不到右大括号后退出解析

我认为浏览器不太可能在闭包的执行部分中添加,但不太可能在缺少的末尾添加}。尽管这绝对不是您想要依赖的行为,并且应该始终很好地形成您的脚本文件。

Why not run some simple tests?

Use some files like this:

<script>
(function a () {
    alert('a');
</script>
<script>
$(function() { alert('b'); a(); });
</script>

This will tell you the following scenarios:

  1. alerts ('a'), alerts('b'), alerts('a') ==> (unlikely): browser completed function definition and wrote closure executing parens

  2. alerts('b'), alerts('a') ==> browser completed function definition and calls went as expected

  3. alerts('b'), throws error for undefined a() ==> browser ignored imcomplete function defintion

  4. does nothing ==> browser quit parsing after not finding a closing brace

I think it would be very unlikely that the browser would add in the executing portion of the closure, but not as unlikely that it would add in the missing end }. Though this is definitely not a behavior you want to count on and should always form your script files nicely.

人生戏 2024-11-25 05:16:13

可能吗?是的。或者不。取决于您准备接受什么假设。

它会发生在当前浏览器中吗? 据我所知,虽然我从未尝试过,但不会发生这种情况。

这是个好主意吗? 不。至少我认为不是。我想不出我想要这个的原因。您能进一步解释为什么会这样做吗?

仅以您发布的示例为例:

1 (function()
2 {
3 function a(n){this.name=n;}
4 var a=this;
5 //more code in here
6 alert(a);
7 //})();

因此浏览器会看到有一个没有结束 }) 的函数表达式,并且因为这样的函数表达式实际上不会对 执行任何操作>(); 最后一点浏览器应该假设这是有意的并也这样做吗?好的,现在应该将其添加到哪一行?如果在第 2、3、4、5 或 6 行之后添加,它将是有效的可执行代码。在我看来,添加缺少的右括号最合乎逻辑的位置是在第 3 行和第 4 行之间,因为这样 < code>var a 声明不会与 function a 定义发生冲突。你想让浏览器解决这类问题吗?

浏览器是否应该在“贪婪”的基础上工作并在外部函数中包含尽可能多的内容?尽可能少?因为你可以打赌 IE 会变得“贪婪”,FF 会变得最小,Chrome 会进行网络搜索,在另一个页面上找到类似但完整的脚本,并使用完整的脚本,而 Opera 只会将其报告为错误,然后不运行它。

Is it possible? Yes. Or No. Depending on what assumptions you are prepared to accept.

Does it happen in current browsers? Not that I know of, though I've never tried it.

Is it a good idea? No. At least, not in my opinion. I can't think of a reason why I would want this. Can you explain further why you would?

Take just the example you posted:

1 (function()
2 {
3 function a(n){this.name=n;}
4 var a=this;
5 //more code in here
6 alert(a);
7 //})();

So the browser sees that there is a function expression that doesn't have a closing }), and since a function expression like that would not actually do anything with the (); bit on the end the browser should assume that that was intended and do that too? OK, now, at which line should it add it in? It would be valid, executable code if added just after any of lines 2, 3, 4, 5 or 6. It seems to me the most logical place to add the missing closing bracket is between lines 3 and 4, because that way the var a declaration wouldn't clash with the function a definition. Do you want the browser to figure that sort of thing out?

Should the browser work on a "greedy" basis and include as much as possible in the outer function? As little as possible? Because you could bet that IE would go "greedy", FF would go minimal, Chrome would do a web search to find a similar but complete script on another page and use the complete one instead, and Opera would just report it as an error and not run it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文