为什么将我的 JavaScript 库包装在匿名函数中可以修复我的竞争条件?

发布于 2024-12-08 04:34:00 字数 1185 浏览 0 评论 0原文

问题:为什么将我的 JavaScript 库包装在匿名函数中可以修复我的竞争条件?

注意:我还对同步和异步加载外部 JavaScript 资源的良好解决方案的评论感兴趣。

我正在开发一个项目,涉及使用 crossrider 编写浏览器扩展。如果您不熟悉跨骑手扩展,它们是用 JavaScript 实现的,您有一个可以与应用程序页面通信的后台页面。应用程序页面可以与每个打开的选项卡一起运行,并且可以操作 DOM。我计划在用户请求打开时远程加载大部分扩展代码。最近,当我向页面 引用 Raphael 添加脚本标签时,我遇到了竞争情况,然后我构建了一些< a href="http://raphaeljs.com/graffle.html" rel="nofollow noreferrer">out of graffle

根据我的理解,如果页面加载了那些已经就位的脚本标签,然后执行将同步发生,尽管由于我附加了脚本标签,所以执行是异步发生的。这与大多数人似乎遇到的问题相反。经过一番研究,我了解到将我的代码 graffle 代码包装在匿名函数中可以修复我的竞争条件。为什么?我读过这篇关于将整个Javascript文件封装在匿名函数中的文章,这似乎与种族没有任何关系条件。

我的调用代码:

var scriptsToLoad   = [ "http://example/Scripts/lib/raphael.js",
                "http://example/Scripts/lib/graffle.js",
                "http://example/Scripts/lib/log4javascript.js"];

for(var i in scriptsToLoad) {
    (function(){
        $("head")
            .append($("<script></script>")
                .attr("type", "text/javascript")
                .attr("src", scriptsToLoad[i]));
    })()
}

Question: Why did wrapping my JavaScript library in an anonymous function fix my race condition?

Note: I am also interested in commentary on good solutions for synchronously and asynchronously loading external JavaScript resources.

I am working on a project that involves writing a browser extension using crossrider. If you are not familiar with cross rider extensions they are implemented in JavaScript, you have a background page that can communicate with an app page. The app page can runs with each open tab and can manipulate the DOM. I plan to load most of the extension code remotely when the user requests it to open. Recently I ran in to a race condition when I added a script tag to the page referencing Raphael, and then something I built out of graffle

From what I understand if a page is loaded with those script tags already in place, then the execution will take place synchronously, although since I was appending the script tags the execution took place asynchronously. This is the opposite problem that most people seem to have. Messing around with it I learned that wrapping my code graffle code in an anonymous function fixed my race condition. Why? I read this post on wrapping whole Javascript files in anonymous functions, and that doesn't seem to having anything to do with race conditions.

My calling code:

var scriptsToLoad   = [ "http://example/Scripts/lib/raphael.js",
                "http://example/Scripts/lib/graffle.js",
                "http://example/Scripts/lib/log4javascript.js"];

for(var i in scriptsToLoad) {
    (function(){
        $("head")
            .append($("<script></script>")
                .attr("type", "text/javascript")
                .attr("src", scriptsToLoad[i]));
    })()
}

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

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

发布评论

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

评论(1

相对绾红妆 2024-12-15 04:34:00

关于您的问题,我不认为对于

关于(a)同步脚本加载,使用 jQuery,执行以下操作:

var scriptsToLoad = [
    "http://example/Scripts/lib/raphael.js",
    "http://example/Scripts/lib/graffle.js",
    "http://example/Scripts/lib/log4javascript.js"
];

$.each(scriptsToLoad, function (index, script) {
    $.ajax({
        url      : script,
        async    : false,
        dataType : 'script'
    });
});

Regarding your question, I do not believe that there has been any particular standard set for the order in which <script /> tags are loaded and evaluated; it is easy to introduce race conditions.

Regarding (a)synchronous script loading, with jQuery, do this:

var scriptsToLoad = [
    "http://example/Scripts/lib/raphael.js",
    "http://example/Scripts/lib/graffle.js",
    "http://example/Scripts/lib/log4javascript.js"
];

$.each(scriptsToLoad, function (index, script) {
    $.ajax({
        url      : script,
        async    : false,
        dataType : 'script'
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文