您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何在 JavaScript 中使用 polyfill?

2026/3/27 10:35:38发布16次查看
javascript 的开发人员总是不断向 javascript 语言添加新功能,以提高性能并添加更好的功能。有时,旧浏览器版本不支持新功能。
例如,es7中引入了指数运算符,对象中的尾随逗号在es7中也有效。现在,在开发应用程序时,我们在应用程序中添加了指数运算符。它将适用于较新版本的浏览器,但如果有人使用非常旧版本的浏览器,他们可能会收到错误,例如浏览器引擎不支持指数运算符。
所以,我们可以使用polyfill来避免这种错误。让我们把polyfill这个词分成两部分来理解它的含义。 poly意味着很多,fill意味着填补空白。这意味着如果浏览器不支持 javascript 的默认功能,则需要通过多种技术来填补浏览器功能的空白。
有两种方法可以解决浏览器不支持功能的问题。一个是polyfill,另一个是transpiler。转译器将代码转换为较低版本,以便浏览器可以支持它。例如,我们可以用 es7 版本的 javascript 编写代码,然后使用转译器将其转换为 es6 或 es5,以使其被旧浏览器支持。
在这里,我们将学习使用 polyfill 概念的不同示例。
语法用户可以按照以下语法使用polyfill概念手动实现javascript方法。
string.prototype.method_name = function (params) { // implement the code for the method // use this keyword to access the reference object}
我们已将方法添加到上述语法中的字符串原型中。 method_name 表示方法名称。我们将带有多个参数的函数分配给该方法。
示例 1(不带 polyfill 的 includes() 方法)在下面的示例中,我们使用了 string 对象的内置 contains() 方法。我们已经定义了字符串并使用includes()方法来检查字符串是否包含特定单词或子字符串。
<html><body> <h2>using the <i>includes() method without polyfill </i> in javascript</h2> <div id = content> </div> <script> let content = document.getelementbyid('content'); let str = you are welcome on tutorialspoint's website. hello users! how are you?; let iswelcome = str.includes('welcome'); content.innerhtml += the original string: + str + <br>; content.innerhtml += the string includes welcome word? + iswelcome + <br>; let isjavascript = str.includes('javascript'); content.innerhtml += the string includes javascript word? + isjavascript + <br>; </script></body></html>
示例 2(带有 polyfill 的 includes() 方法)在上面的例子中,我们使用了内置的includes()方法。在这个例子中,我们将为includes()方法定义polyfill。如果任何浏览器不支持includes()方法,它将执行用户定义的includes()方法。
这里,我们将includes()方法添加到字符串对象的原型中。在函数中,如果搜索字符串是正则表达式类型,我们会抛出错误。另外,“pos”参数是一个选项,因此如果用户不传递它,则将其视为零。最后,使用indexof()方法检查字符串是否包含该单词,并根据该结果返回一个布尔值。
<html><body> <h2>using the <i>includes() method with polyfill </i> in javascript</h2> <div id = content> </div> <script> let content = document.getelementbyid('content'); string.prototype.includes = function (str, pos) { // first, check whether the first argument is a regular expression if (str instanceof regexp) { throw error(search string can't be an instance of regular expression); } // second parameter is optional. so, if it isn't passed as an argument, consider it zero if (pos === undefined) { pos = 0; } content.innerhtml += the user-defined includes method is invoked! <br> // check if the index of the string is greater than -1. if yes, string includes the search string. return this.indexof(str, pos) !== -1; }; let str = this is a testing string. use this string to test includes method.; content.innerhtml += `the original string is ` + str +` <br>`; let istesting = str.includes('testing'); content.innerhtml += the string includes testing word? + istesting + <br>; let isyour = str.includes('your'); content.innerhtml += the string includes your word? + isyour + <br>; </script></body></html>
示例3(为filter()方法实现polyfill)我们在下面的示例中为filter()方法实现了polyfill。我们首先确保引用数组不为空并且回调是一种函数。之后,我们迭代数组并为每个数组值执行回调函数。如果回调函数返回 true,我们将其推送到输出数组。最后,我们返回包含过滤值的输出数组
<html><body> <h2>using the <i> filter() method with polyfill </i> in javascript</h2> <div id = content> </div> <script> let content = document.getelementbyid('content'); array.prototype.filter = function (callback) { // check if the reference array is not null if (this === null) throw new error; // check that callback is a type of function if (typeof callback !== function) throw new error; var output = []; // iterate through array for (var k = 0; k < this.length; k++) { // get value from index k var val = this[k]; // call the callback function and, based on a returned boolean value, push the array value in the output array if (callback.call(this, val, k)) { output.push(val); } } return output; }; function getdivisibleby10(val, k) { // return true if val is divisible by 10. if (val % 10 == 0) { return true; } return false; } let array = [10, 20, 40, 65, 76, 87, 90, 80, 76, 54, 32, 23, 65, 60]; let filtered = array.filter(getdivisibleby10); content.innerhtml += the original array is + json.stringify(array) + <br>; content.innerhtml += the filtered array is + json.stringify(filtered) + <br>; </script></body></html>
本教程教我们如何实现includes() 和filter() 方法的polyfill。但是,用户可以使用 if-else 语句来检查浏览器是否支持特定方法。如果没有,则执行用户定义的方法,否则执行内置方法。
以上就是如何在 javascript 中使用 polyfill?的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product