如何提供的ECMAScript 5(ES 5)-shim? [英] How to provide ECMAScript 5 (ES 5)-shim?

查看:193
本文介绍了如何提供的ECMAScript 5(ES 5)-shim?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的ECMAScript第五版(发布2009年12月)引入了许多新的方法(见详情此表 )。但是,仍然有旧的浏览器在那里不实现这些新方法。

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.

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

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

不过,我不知道如何提供ES5-垫片......我是不是应该给它所有的浏览器,像这样:

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>

(我使用 Function.prototype.bind 来检查,如果浏览器实现了所有新的ECMAScript 5的方法。根据我上面链接,<上的兼容性表code>绑定是最后的堡垒,当涉及到实施的ECMAScript 5的方法。)

(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.)

当然,对于这个垫片是有效的,它必须执行的的所有其它脚本,这意味着我们要在页面中包含上述SCRIPT早期元素(HEAD所有其它脚本元素之前)。

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).

所以,将这个第二个例子是一个很好的方式来提供的ECMAScript 5垫片到浏览器?有没有更好的办法做到这一点?

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

推荐答案

ES5,沉将只在该浏览器没有实现垫片部件,所以只要给它所有的浏览器。它会处理的需要进行匀什么检测,而不是什么呢。

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...

这篇关于如何提供的ECMAScript 5(ES 5)-shim?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆