如何提供 ECMAScript 5 (ES 5)-shim?

发布于 2024-12-24 21:08:28 字数 1158 浏览 2 评论 0原文

ECMAScript 第五版(2009 年 12 月发布)引入了一系列新方法(请参阅此表了解详情)。然而,仍然有一些旧的浏览器没有实现这些新方法。

幸运的是,有一个方便的脚本(用 JavaScript 编写) - ES5-shim - 它实现了这些方法在它们不存在的环境中手动。

但是,我不确定如何提供 ES5-shim...我是否应该将其“提供”给所有浏览器,如下所示:

<script src="es5-shim.js"></scipt>

或者我应该包含一个检查以便仅“打扰”那些真正需要它的浏览器,例如所以:(

<script>
    if ( !Function.prototype.hasOwnProperty( 'bind' ) ) {
        (function () {
            var shim = document.createElement( 'script' );
            shim.src = 'es5-shim.js';
            var script = document.getElementsByTagName( 'script' )[0];
            script.parentNode.insertBefore( shim, script );
        }());
    }
</script>

我使用 Function.prototype.bind 来检查浏览器是否实现了所有新的 ECMAScript 5 方法。根据我上面链接的兼容性表,bind 是“最后的堡垒”当涉及到实现 ECMAScript 5 方法时。)

当然,为了使这个 shim 有效,它必须在所有其他脚本之前执行,这意味着我们想要包含上面提到的 SCRIPT页面早期的元素(在 HEAD 中,在所有其他 SCRIPT 元素之前)。

那么,第二个示例是向浏览器提供 ECMAScript 5-shim 的好方法吗?有更好的方法吗?

ECMAScript Fifth Edition (released December 2009) introduces a bunch of new methods (see this table for details). However, there still are older browsers out there which do not implement those new methods.

Luckily, there exists a convenient script (written in JavaScript) - ES5-shim - which implements those methods manually in environments where they don't exist.

However, I am not sure how to provide ES5-shim... Should I just "give" it to all browsers, like so:

<script src="es5-shim.js"></scipt>

Or should I include a check in order to only "bother" those browsers which really need it, like so:

<script>
    if ( !Function.prototype.hasOwnProperty( 'bind' ) ) {
        (function () {
            var shim = document.createElement( 'script' );
            shim.src = 'es5-shim.js';
            var script = document.getElementsByTagName( 'script' )[0];
            script.parentNode.insertBefore( shim, script );
        }());
    }
</script>

(I'm using Function.prototype.bind to check if a browser implements all new ECMAScript 5 methods. According to the compatibility table which I linked above, bind is the "last bastion" when it comes to implementing ECMAScript 5 methods.)

Of course, for this shim to be effective, it has to be executed before all other scripts, which means that we want to include the above mentioned SCRIPT elements early in the page (in the HEAD, before all other SCRIPT elements).

So, would this second example be a good way to provide ECMAScript 5-shim to browsers? Is there a better way to do it?

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

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

发布评论

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

评论(3

夜灵血窟げ 2024-12-31 21:08:28

ES5-Shim 只会填充浏览器未实现的部分,因此只需将其提供给所有浏览器即可。它将处理需要填充和不需要填充的内容的检测。

但请注意列出的关于哪些垫片在某些情况下无法正常工作的警告。我过去曾遇到过这个问题,它会引起很大的痛苦,直到你意识到答案非常简单......

ES5-Shim will only shim parts that the browsers don't implement, so just give it to all browsers. It'll handle the detection of what needs to be shimmed and what doesn't.

But pay attention to the caveats listed on what shims don't work correctly in some instances. I've had issues with that in the past and it causes a ton of pain until you realize the answer was super simple...

棒棒糖 2024-12-31 21:08:28

这似乎对我有用:

<!--[if lt IE 9]><script src="java/es5-shim.min.js"></script><![endif]-->

This seems to work for me:

<!--[if lt IE 9]><script src="java/es5-shim.min.js"></script><![endif]-->
往昔成烟 2024-12-31 21:08:28

目前,最适合 ES5-Shim 的解决方案是在所有环境中使用该库,并允许它在运行时检测需要修补哪些功能。最好从社区 CDN 提供它,以最大限度地提高跨站点缓存命中率。

话虽如此,有一个开放的机会来创建结合了特征检测、代理指纹识别和动态捆绑的系统,以自动生成和交付目标垫片子集。该问题的范围远远超出了 ES5-Shim 的范围,并且可以应用于所有类型的垫片。

At present, the solution that works best with ES5-Shim is to use the library in all environments and allow it to detect which features it needs to patch at run-time. It would be even better to deliver it from a community CDN to maximize cross-site cache hits.

That being said, there is an open opportunity to create systems that combines feature detection, agent fingerprinting, and dynamic bundling to automatically generate and deliver targeted shim subsets. The scope of the problem extends far beyond just ES5-Shim and could be applied to all sorts of shims.

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