HTML5 中的 polyfill 的含义是什么?

发布于 2024-11-30 02:43:16 字数 403 浏览 2 评论 0原文

HTML5 中的 polyfill 的含义是什么?我在很多关于 HTML5 的网站上看到这个词,例如 HTML5-Cross-Browser- Polyfill。

所以我们在这里收集所有的垫片、回退和填充 为了在不支持的浏览器中植入 HTML5 功能 本身就支持它们。

其实我不太明白polyfills是什么意思。

它是一种新的 HTML5 技术还是 JavaScript 库?在 HTML5 之前我从未听说过这个词。

垫片、回退和填充之间有什么区别?

What is the meaning of polyfills in HTML5? I saw this word in many sites about HTML5, e.g. HTML5-Cross-Browser-Polyfills.

So here we're collecting all the shims, fallbacks, and polyfills
in order to implant HTML5 functionality in browsers that don't
natively support them.

I actually don't understood what is the meaning of polyfills.

Is it a new HTML5 technique or a JavaScript library? I never heard this word before HTML5.

And what is the difference between shims, fallbacks, and polyfills?

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

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

发布评论

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

评论(6

紙鸢 2024-12-07 02:43:16

Polyfill 是一种用 JavaScript 制作的浏览器回退,它允许您期望在现代浏览器中工作的功能在旧版浏览器中工作,例如,在旧版浏览器中支持画布(HTML5 功能)。

它是一种 HTML5 技术,因为它与 HTML5 结合使用,但它不是 HTML5 的一部分,并且您可以在没有 HTML5 的情况下使用 Polyfill(例如,支持您想要的 CSS3 技术)。

这是一篇好文章:

http://remysharp.com/2010/10/08/what- is-a-polyfill/

这是 Polyfill 和 Shims 的完整列表:

https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills

A polyfill is a browser fallback, made in JavaScript, that allows functionality you expect to work in modern browsers to work in older browsers, e.g., to support canvas (an HTML5 feature) in older browsers.

It's sort of an HTML5 technique, since it is used in conjunction with HTML5, but it's not part of HTML5, and you can have polyfills without having HTML5 (for example, to support CSS3 techniques you want).

Here's a good post:

http://remysharp.com/2010/10/08/what-is-a-polyfill/

Here's a comprehensive list of Polyfills and Shims:

https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-browser-Polyfills

铁轨上的流浪者 2024-12-07 02:43:16

首先让我们澄清一下 polyfil 不是什么:polyfill 不是 HTML5 标准的一部分。 Polyfill 也不限于 Javascript,尽管您经常看到在这些上下文中提到 Polyfill。

术语“polyfill”本身指的是一些代码,“允许您拥有一些您期望在当前或“现代”浏览器中使用的特定功能,这些功能也可以在其他不支持该功能内置的浏览器中使用。”

的来源和示例此处的polyfill:

http://www.programmerinterview.com/index.php/html5/html5-polyfill/

First off let's clarify what a polyfil is not: A polyfill is not part of the HTML5 Standard. Nor is a polyfill limited to Javascript, even though you often see polyfills being referred to in those contexts.

The term polyfill itself refers to some code that "allows you to have some specific functionality that you expect in current or “modern” browsers to also work in other browsers that do not have the support for that functionality built in. "

Source and example of polyfill here:

http://www.programmerinterview.com/index.php/html5/html5-polyfill/

初雪 2024-12-07 02:43:16

Polyfill 是一段代码(或插件),它提供了您(开发人员)期望浏览器本机提供的技术。

A polyfill is a piece of code (or plugin) that provides the technology that you, the developer, expect the browser to provide natively.

流云如水 2024-12-07 02:43:16

Polyfill 是一个垫片,它用对垫片的调用来替换原始调用。

例如,假设您想使用 navigator.mediaDevices 对象,但并非所有浏览器都支持此对象。您可以想象一个提供了垫片的库,您可以像这样使用它:

<script src="js/MediaShim.js"></script>
<script>
    MediaShim.mediaDevices.getUserMedia(...);
</script>

在这种情况下,您显式调用垫片而不是使用原始对象或方法。另一方面,polyfill 替换了原始对象上的对象和方法。

例如:

<script src="js/adapter.js"></script>
<script>
    navigator.mediaDevices.getUserMedia(...);
</script>

在您的代码中,看起来好像您正在使用标准 navigator.mediaDevices 对象。但实际上,polyfill(示例中的adapter.js)已经用它自己的对象替换了这个对象。

它所取代的是垫片。这将检测该功能是否是本机支持的,如果是,则使用它,或者如果不是,它将使用其他 API 来解决它。

因此,polyfill 是一种“透明”垫片。这就是 Remy Sharp(创造了该术语的人)所说的“如果您删除了 polyfill 脚本,您的代码将继续工作,尽管删除了 polyfill,但不需要任何更改”时的意思。

A polyfill is a shim which replaces the original call with the call to a shim.

For example, say you want to use the navigator.mediaDevices object, but not all browsers support this. You could imagine a library that provided a shim which you might use like this:

<script src="js/MediaShim.js"></script>
<script>
    MediaShim.mediaDevices.getUserMedia(...);
</script>

In this case, you are explicitly calling a shim instead of using the original object or method. The polyfill, on the other hand, replaces the objects and methods on the original objects.

For example:

<script src="js/adapter.js"></script>
<script>
    navigator.mediaDevices.getUserMedia(...);
</script>

In your code, it looks as though you are using the standard navigator.mediaDevices object. But really, the polyfill (adapter.js in the example) has replaced this object with its own one.

The one it has replaced it with is a shim. This will detect if the feature is natively supported and use it if it is, or it will work around it using other APIs if it is not.

So a polyfill is a sort of "transparent" shim. And this is what Remy Sharp (who coined the term) meant when saying "if you removed the polyfill script, your code would continue to work, without any changes required in spite of the polyfill being removed".

月依秋水 2024-12-07 02:43:16

Web 开发中的polyfill 是在不支持该功能的 Web 浏览器上实现该功能的代码。它通常指的是实现 HTML5 或 CSS Web 标准的 JavaScript 库,该标准可以是现有浏览器上的既定标准(受某些较旧的浏览器支持)或提议的标准(不受任何浏览器支持)。根据定义,“polyfill 是浏览器 API 的填充程序”。

HTML5 标准不包含polyfill。 Polyfill 并不限于 Javascript,尽管事实上它们经常在该上下文中被提及。

A polyfill in web development is code that implements a feature on web browsers that do not support the feature. It usually refers to a JavaScript library that implements an HTML5 or CSS web standard, either an established standard (supported by some older browsers) or a proposed standard (not supported by any browsers) on existing browsers. "A polyfill is a shim for a browser API," according to the definition.

The HTML5 Standard does not include a polyfill. Polyfills are not limited to Javascript, despite the fact that they are frequently mentioned in that context.

不可一世的女人 2024-12-07 02:43:16

除了其他答案之外,以下是一些可能有帮助的高级想法和信息。

Pollyfills 就像特定浏览器的兼容性补丁。
垫片是对特定参数的更改。
如果 @mediaquery 与浏览器不兼容,则可以使用后备。

这在某种程度上取决于您的应用程序/网站需要兼容的要求。

您可以检查此站点以了解特定库与特定浏览器的兼容性。
https://caniuse.com/

Here are some high level thoughts and info that might help, aside from the other answers.

Pollyfills are like a compatability patch for specific browsers.
Shims are changes to specific arguments.
Fallbacks can be used if say a @mediaquery is not compatible with a browser.

It kind of depends on the requirements of what your app/website needs to be compatible with.

You cna check this site out for compatability of specific libraries with specific browsers.
https://caniuse.com/

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