简体   繁体   English

为什么旧浏览器不支持 JavaScript 中的新内置方法?

[英]Why are new built-in methods in javascript not supported by older browsers?

Why are some built-in methods in JavaScript not supported by older browsers ?为什么旧浏览器不支持 JavaScript 中的某些内置方法?

For example: The Array fill() method: (browser support: chrome[v.45], IE[v.12.0], Opera[v.32.0], etc )例如:Array fill()方法:(浏览器支持:chrome[v.45]、IE[v.12.0]、Opera[v.32.0] 等)

Out of curiosity I decided to "reinvent the wheel" and recreate the above method:出于好奇,我决定“重新发明轮子”并重新创建上述方法:

<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>

Array.prototype.myFunction = function (staticValue, start, end) {
  for(var i = 0; i< this.length; i++) {

     if(i===start) {
         for(start= i; i<end+1; i++) {
             this[i] = staticValue;
         };
     };

     if(start == undefined && end == undefined) {
         this[i] = staticValue;
     };

  };

};

var testFruits = ["Banana", "Orange", "Apple", "Mango", 456, 48999, "power"];

var cars = ["Saab", "Volvo", "BMW"];

testFruits.myFunction("Kiwi", 1, 3);

cars.myFunction("Voom!");

document.getElementById("demo").innerHTML =
testFruits + "<br />" + "<br />"  +cars;

</script>

</body>
</html>

Does this mean that by recreating some new but poorly supported built-in methods make your code completely supported by old browsers ?这是否意味着通过重新创建一些新的但支持不佳的内置方法使您的代码完全受旧浏览器支持?

My theory is YES, considering that I only used regular functions and common techniques to come up with a solution.我的理论是肯定的,考虑到我只使用常规函数和常用技术来提出解决方案。

Older browsers are old .较旧的浏览器很 The people who implemented the older browsers don't go back and add new features.实现旧浏览器的人不会回去添加新功能。

If a JavaScript language feature was standardized in 2015, then a browser released in 2012 just doesn't have it because time travel is not common.如果 JavaScript 语言功能在 2015 年标准化,那么 2012 年发布的浏览器就没有它,因为时间旅行并不常见。 It's not impossible that somebody might release an update for the old browser, but then it wouldn't be an old browser anymore.有人可能会发布旧浏览器的更新并非不可能,但它就不再是旧浏览器了。

Plain JavaScript implementations of new features are sometimes possible, and those are usually called "polyfills".新功能的普通 JavaScript 实现有时是可能的,这些通常称为“polyfills”。 Many MDN pages include such code ( one example ).许多 MDN 页面都包含这样的代码( 一个例子)。 Some language features, however, involve new syntax, and that cannot be reproduced with simple JavaScript code of course.然而,一些语言特性涉及新的语法,当然不能用简单的 JavaScript 代码重现。

In practical sense and if you use compilation for your project you may consider to add at the top of your entry point实际上,如果您为项目使用编译,则可以考虑在入口点的顶部添加

import 'core-js'

At the moment core-js polyfill library is the easiest way to make Cross Browser Support目前core-js polyfill 库是实现跨浏览器支持的最简单方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM