HTML5中polyfills的含義是什麼?

本文翻譯自:What is the meaning of polyfills in HTML5?

What is the meaning of polyfills in HTML5? HTML5中polyfills的含義是什麼? I saw this word in many sites about HTML5, eg HTML5-Cross-Browser-Polyfills. 我在許多網站上看到過關於HTML5的這個詞,例如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. 所以我們在這裏收集所有墊片,後備和polyfill,以便在本身不支持它們的瀏覽器中植入HTML5功能。

I actually don't understood what is the meaning of polyfills. 我其實不明白polyfills是什麼意思。

Is it a new HTML5 technique or a JavaScript library? 它是一種新的HTML5技術還是JavaScript庫? I never heard this word before HTML5. 我從來沒有在HTML5之前聽過這個詞。

And what is the difference between shims, fallbacks, and polyfills? 墊片,後備和填充物之間有什麼區別?


#1樓

參考:https://stackoom.com/question/Tjjn/HTML-中polyfills的含義是什麼


#2樓

First off let's clarify what a polyfil is not: A polyfill is not part of the HTML5 Standard. 首先讓我們澄清一下polyfil不是什麼:polyfill不是HTML5標準的一部分。 Nor is a polyfill limited to Javascript, even though you often see polyfills being referred to in those contexts. 即使您經常在這些上下文中看到polyfill被引用,但polyfill也不限於Javascript。

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. " 術語“polyfill”本身指的是一些代碼,“允許您在當前或”現代“瀏覽器中使用某些特定功能,以便在其他不支持內置功能的瀏覽器中工作。”

Source and example of polyfill here: 這裏的polyfill的來源和例子:

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


#3樓

polyfill是一段代碼(或插件),它提供了開發人員期望瀏覽器本機提供的技術。


#4樓

A polyfill is a shim which replaces the original call with the call to a shim. polyfill是一種墊片,它通過調用墊片來替換原始調用。

For example, say you want to use the navigator.mediaDevices object, but not all browsers support this. 例如,假設您要使用navigator.mediaDevices對象,但並非所有瀏覽器都支持此對象。 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. 另一方面,polyfill替換原始對象上的對象和方法。

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. 在您的代碼中,它看起來好像您正在使用標準的navigator.mediaDevices對象。 But really, the polyfill (adapter.js in the example) has replaced this object with its own one. 但實際上,polyfill(示例中的adapter.js)已將此對象替換爲自己的對象。

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. 這將檢測該功能是否是本機支持的,如果是,則使用它,如果不是,它將使用其他API解決它。

So a polyfill is a sort of "transparent" shim. 因此,polyfill是一種“透明”的墊片。 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 ". 這就是Remy Sharp(創造這個術語)的意思,當說“ 如果你刪除了polyfill腳本,你的代碼將繼續工作,儘管polyfill被刪除,但不需要任何改變 ”。


#5樓

A polyfill is a browser fallback, made in JavaScript, that allows functionality you expect to work in modern browsers to work in older browsers, eg, to support canvas (an HTML5 feature) in older browsers. polyfill是一種瀏覽器後備,由JavaScript構建,允許您希望在現代瀏覽器中工作的功能在舊版瀏覽器中工作,例如,支持舊版瀏覽器中的canvas(HTML5功能)。

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). 它是一種HTML5技術,因爲它與HTML5結合使用,但它不是HTML5的一部分,你可以在沒有HTML5的情況下使用polyfill(例如,支持你想要的CSS3技術)。

Here's a good post: 這是一篇好文章:

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

Here's a comprehensive list of Polyfills and Shims: 以下是Polyfill和Shims的完整列表:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章