a polyfill is a shim for a browser API.

摘自http://www.2ality.com/2011/12/shim-vs-polyfill.html
翻译版http://www.cnblogs.com/ziyunfei/archive/2012/09/17/2688829.html

In the JavaScript world, one frequently encounters the words shim and polyfill. What are those things and what is the difference between them?Shim. A shim is a library that brings a new API to an older environment, using only the means of that environment.
Polyfill. In October 2010, Remy Sharp blogged about the term “polyfill” [via Rick Waldron]:
A polyfill is a piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively. Flattening the API landscape if you will.
Thus, a polyfill is a shim for a browser API. You typically check if a browser supports an API and load a polyfill if it doesn’t. That allows you to use the API in either case. The term polyfill comes from a home improvement product (quoting Remy Sharp):

Polyfilla is a UK product known as Spackling Paste in the US. With that in mind: think of the browsers as a wall with cracks in it. These [polyfills] help smooth out the cracks and give us a nice smooth wall of browsers to work with.

Polyfilla – image from tooled-up.com [via Paul]

Examples. Paul Irish has published a list with “HTML5 Cross Browser Polyfills”. es5-shimis an example of a (non-polyfill) shim – it retrofits ECMAScript 5 features on ECMAScript 3 engines. It is purely language-related and makes just as much sense on Node.js as it does on browsers.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容